Skip to content

Commit

Permalink
Merge pull request #717 from AlreadyM/ch08-02-strings-fix
Browse files Browse the repository at this point in the history
修正西里尔字母部分的翻译语义通畅
  • Loading branch information
KaiserY committed Apr 18, 2023
2 parents 69622d9 + 0f6e444 commit abfa931
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/ch08-02-strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ let hello = "Здравствуйте";
let answer = &hello[0];
```

我们已经知道 `answer` 不是第一个字符 `З`。当使用 UTF-8 编码时,`З` 的第一个字节 `208`,第二个是 `151`,所以 `answer` 实际上应该是 `208`,不过 `208` 自身并不是一个有效的字母。返回 `208` 可不是一个请求字符串第一个字母的人所希望看到的,不过它是 Rust 在字节索引 0 位置所能提供的唯一数据。用户通常不会想要一个字节值被返回。即使这个字符串只有拉丁字母,如果 `&"hello"[0]` 是返回字节值的有效代码,它也会返回 `104` 而不是 `h`
我们已经知道 `answer`的第一个字符不是数字`3`。当使用 UTF-8 编码时,第一个字节(西里尔字母的 Ze)`З` 的编码是 `208`,第二个是 `151`,所以 `answer` 实际上应该是 `208`,不过 `208` 自身并不是一个有效的字母。返回 `208` 可不是一个请求字符串第一个字母的人所希望看到的,不过它是 Rust 在字节索引 0 位置所能提供的唯一数据。用户通常不会想要一个字节值被返回。即使这个字符串只有拉丁字母,如果 `&"hello"[0]` 是返回字节值的有效代码,它也会返回 `104` 而不是 `h`

为了避免返回意外的值并造成不能立刻发现的 bug,Rust 根本不会编译这些代码,并在开发过程中及早杜绝了误会的发生。

Expand Down

0 comments on commit abfa931

Please sign in to comment.