Skip to content

Commit e610f7d

Browse files
authored
Merge pull request #277 from leque/fix-strings
fix strings.md
2 parents f86f652 + 802889b commit e610f7d

File tree

3 files changed

+80
-77
lines changed

3 files changed

+80
-77
lines changed

1.6/ja/book/strings.md

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,27 @@
66
<!-- focus. Any time you have a data structure of variable size, things can get -->
77
<!-- tricky, and strings are a re-sizable data structure. That being said, Rust’s -->
88
<!-- strings also work differently than in some other systems languages, such as C. -->
9-
文字列は、プログラマがマスタすべき重要なコンセプトです
10-
Rustの文字列の扱いは、Rust言語がシステムにフォーカスしているため、少し他の言語と異なります。
11-
動的なサイズを持つデータ構造が有る時、常に物事は複雑になります
9+
文字列は、プログラマがマスタすべき重要な概念です
10+
Rustの文字列の扱いは、Rust言語がシステムプログラミングにフォーカスしているため、少し他の言語と異なります。
11+
動的なサイズを持つデータ構造があるといつも、物事は複雑性を孕みます
1212
そして文字列もまたサイズを変更することができるデータ構造です。
13-
これはつまり、Rustの文字列もまた、Cのような他のシステム言語とは少し異なるということです
13+
これはつまり、Rustの文字列もまた、Cのような他のシステム言語とは少し異なる振る舞いをするということです
1414

1515

1616
<!-- Let’s dig into the details. A ‘string’ is a sequence of Unicode scalar values -->
1717
<!-- encoded as a stream of UTF-8 bytes. All strings are guaranteed to be a valid -->
1818
<!-- encoding of UTF-8 sequences. Additionally, unlike some systems languages, -->
1919
<!-- strings are not null-terminated and can contain null bytes. -->
20-
詳しく見ていきましょう「文字列」は、UTF-8のバイトストリームとしてエンコードされたユニコードのスカラ値のシーケンスです。
21-
すべての文字列は、正しくエンコードされたUTF-8のシーケンスであることが保証されています。
22-
また、他のシステム言語とはことなり、文字列はnull終端でなく、nullバイトを含むことが可能です
20+
詳しく見ていきましょう「文字列」は、UTF-8のバイトストリームとしてエンコードされたユニコードのスカラ値のシーケンスです。
21+
すべての文字列は、妥当なUTF-8のシーケンスであることが保証されています。
22+
また、他のシステム言語とは異なり、文字列はnull終端でなく、nullバイトを保持することもできます
2323

2424
<!-- Rust has two main types of strings: `&str` and `String`. Let’s talk about -->
2525
<!-- `&str` first. These are called ‘string slices’. A string slice has a fixed -->
2626
<!-- size, and cannot be mutated. It is a reference to a sequence of UTF-8 bytes. -->
27-
Rustは主要な文字列型を二種類持っています: `&str``String`です。
28-
まず `&str` について説明しましょう。 `&str` は「文字列のスライス」と呼ばれます。
29-
文字列のスライスは固定のサイズを持ち、変更不可能です。そして、文字列のスライスはUTF-8のバイトシーケンスへの参照です。
27+
Rustには主要な文字列型が二種類あります。`&str``String`です。
28+
まず `&str` について説明しましょう。 `&str` は「文字列スライス」と呼ばれます。
29+
文字列スライスは固定サイズで変更不可能です。文字列スライスはUTF-8のバイトシーケンスへの参照です。
3030

3131

3232
```rust
@@ -38,16 +38,17 @@ let greeting = "Hello there."; // greeting: &'static str
3838
<!-- inside our compiled program, and exists for the entire duration it runs. The -->
3939
<!-- `greeting` binding is a reference to this statically allocated string. Any -->
4040
<!-- function expecting a string slice will also accept a string literal. -->
41-
`"Hello there."` は文字列リテラルであり、 `&'static str` 型を持ちます。
42-
文字列リテラルは、静的にアロケートされた文字列のスライスであり、これはつまりコンパイルされたプログラム中に保存されており、
43-
プログラムの実行中全てにわたって存在していることを意味しています。
44-
どの文字列のスライスを引数として期待している関数も、文字列リテラルを引数に取ることができます。
41+
`"Hello there."` は文字列リテラルで、 `&'static str` 型を持ちます。
42+
文字列リテラルは、静的にアロケートされた文字列スライスです。これはつまりコンパイルされたプログラム内に保存されていて、
43+
プログラムの実行中全てにわたって存在しているということです。
44+
`greeting`の束縛はこのように静的にアロケートされた文字列を参照しています。
45+
文字列スライスを引数として期待している関数はすべて文字列リテラルを引数に取ることができます。
4546

4647
<!-- String literals can span multiple lines. There are two forms. The first will -->
4748
<!-- include the newline and the leading spaces: -->
48-
文字列リテラルは複数行に渡ることができます
49-
複数行の文字列リテラルを記述する方法は、2つの形式があります
50-
一つ目の形式は、改行と行頭の空白を文字列に含む形式です:
49+
文字列リテラルは複数行にわたることができます
50+
複数行文字列リテラルには2つの形式があります
51+
一つ目の形式は、改行と行頭の空白を含む形式です:
5152

5253
```rust
5354
let s = "foo
@@ -57,7 +58,7 @@ assert_eq!("foo\n bar", s);
5758
```
5859

5960
<!-- The second, with a `\`, trims the spaces and the newline: -->
60-
もう一つは `\` を用いて、空白と改行を削る形式です:
61+
もう一つは `\` を使って空白と改行を削る形式です:
6162

6263
```rust
6364
let s = "foo\
@@ -70,9 +71,9 @@ assert_eq!("foobar", s);
7071
<!-- This string is growable, and is also guaranteed to be UTF-8. `String`s are -->
7172
<!-- commonly created by converting from a string slice using the `to_string` -->
7273
<!-- method. -->
73-
Rustは `&str` だけでなく、 `String` というヒープにアロケートされる文字列の形式も持っています
74-
この文字列は伸張可能であり、またUTF-8であることが保証されています
75-
`String` は一般的に文字列のスライスをメソッド `to_string` を用いて変換することで生成されます
74+
Rustには `&str` だけでなく、 `String` というヒープアロケートされる文字列もあります
75+
この文字列は伸張可能であり、またUTF-8であることも保証されています
76+
`String` は一般的に文字列スライスを `to_string` メソッドで変換することで作成されます
7677

7778
```rust
7879
let mut s = "Hello".to_string(); // mut s: String
@@ -100,9 +101,9 @@ fn main() {
100101
<!-- instead of `&str`. For example, [`TcpStream::connect`][connect] has a parameter -->
101102
<!-- of type `ToSocketAddrs`. A `&str` is okay but a `String` must be explicitly -->
102103
<!-- converted using `&*`. -->
103-
このような変換は `&str` の代わりに、 `&str` のトレイトを引数として期待している関数では自動的には行われません
104+
このような変換は `&str` ではなく `&str` の実装するトレイトを引数として取る関数に対しては自動的には行われません
104105
たとえば、 [`TcpStream::connect`][connect] は引数として型 `ToSocketAddrs` を要求しています。
105-
このような関数には `&str` は渡せますが、 `String``&*` を用いて明示的に変換する必要があります
106+
このような関数には `&str` は渡せますが、 `String``&*` を用いて明示的に変換しなければなりません
106107

107108
```rust,no_run
108109
use std::net::TcpStream;
@@ -117,14 +118,14 @@ TcpStream::connect(&*addr_string); // addr_string を &str に変換して渡す
117118

118119
<!-- Viewing a `String` as a `&str` is cheap, but converting the `&str` to a -->
119120
<!-- `String` involves allocating memory. No reason to do that unless you have to! -->
120-
`String``&str` として見るコストは低いですが`&str``String` に変換するのはメモリのアロケーションを発生させます
121-
必要がなければ、やるべきではないでしょう!
121+
`String``&str` として見るコストは低いのですが`&str``String` に変換するとメモリアロケーションが発生します
122+
必要がなければ、やるべきではないでしょう
122123

123124
<!-- ## Indexing -->
124125
## インデクシング
125126

126127
<!-- Because strings are valid UTF-8, strings do not support indexing: -->
127-
文字列が正しいUTF-8であるため、文字列はインデクシングをサポートしていません:
128+
文字列は妥当なUTF-8であるため、文字列はインデクシングをサポートしていません:
128129

129130
```rust,ignore
130131
let s = "hello";
@@ -140,11 +141,11 @@ println!("The first letter of s is {}", s[0]); // エラー!!!
140141
<!-- isn’t something defined in Unicode, exactly. We can choose to look at a string as -->
141142
<!-- individual bytes, or as codepoints:-->
142143
普通、ベクタへの `[]` を用いたアクセスはとても高速です。
143-
しかし、UTF-8にエンコードされた文字列中の一つ一つの文字は複数のバイトであることが可能なため
144-
文字列のn番の文字を探すためには文字列上を移動していく必要があります
145-
そのような作業はとても高コストな演算であり、誤解してはならない点です
146-
さらに言えば、「文字」というものはUnicodeにおいては正確に定義されていません
147-
文字列を、それぞれのバイトとして見ることも、コードポイントの集まりとして見ることもできるのです
144+
しかし、UTF-8でエンコードされた文字列内の文字は複数のバイト対応することがあるため
145+
文字列のn番目の文字を探すには文字列上を走査していく必要があります
146+
そのような処理はベクタのアクセスに比べると非常に高コストな演算であり、誤解を招きたくなかったのです
147+
さらに言えば、上の「文字 (letter)」というのはUnicodeでの定義と厳密には一致しません
148+
文字列をバイト列として見るかコードポイント列として見るか選ぶことができます
148149

149150
```rust
150151
let hachiko = "忠犬ハチ公";
@@ -182,12 +183,12 @@ let dog = hachiko.chars().nth(1); // hachiko[1]のような感じで
182183
```
183184

184185
<!-- This emphasizes that we have to walk from the beginning of the list of `chars`. -->
185-
このコードは、`chars` のリストの上を移動している事を強調しています
186+
このコードは、`chars` のリストの上を先頭から走査しなければならないことを強調しています
186187

187188
## スライシング
188189

189190
<!-- You can get a slice of a string with slicing syntax: -->
190-
文字列のスライスは以下のようにスライス構文を用いて取得することができます:
191+
文字列スライスは以下のようにスライス構文を使って取得することができます:
191192

192193
```rust
193194
let dog = "hachiko";
@@ -214,10 +215,10 @@ character boundary'
214215
```
215216

216217
<!-- ## Concatenation -->
217-
## 結合
218+
## 連結
218219

219220
<!-- If you have a `String`, you can concatenate a `&str` to the end of it: -->
220-
`String` が存在するとき、 `&str` を末尾に結合することができます:
221+
`String` が存在するとき、 `&str` を末尾に連結することができます:
221222

222223
```rust
223224
let hello = "Hello ".to_string();
@@ -227,7 +228,7 @@ let hello_world = hello + world;
227228
```
228229

229230
<!-- But if you have two `String`s, you need an `&`: -->
230-
しかし、2つの `String` を結合するには`&` が必要になります:
231+
しかし、2つの `String` を連結するには`&` が必要になります:
231232

232233
```rust
233234
let hello = "Hello ".to_string();
@@ -239,7 +240,7 @@ let hello_world = hello + &world;
239240
<!-- This is because `&String` can automatically coerce to a `&str`. This is a -->
240241
<!-- feature called ‘[`Deref` coercions][dc]’. -->
241242
これは、 `&String` が自動的に `&str` に型強制されるためです。
242-
このフィーチャーは[`Deref` による型強制][dc] 」と呼ばれています。
243+
このフィーチャは[`Deref` による型強制][dc] 」と呼ばれています。
243244

244245
[dc]: deref-coercions.html
245246
[connect]: ../std/net/struct.TcpStream.html#method.connect

0 commit comments

Comments
 (0)