1
- % Strings
1
+ % 文字列
2
+ <!-- % Strings -->
3
+
4
+ <!-- Strings are an important concept for any programmer to master. Rust’s string -->
5
+ <!-- handling system is a bit different from other languages, due to its systems -->
6
+ <!-- focus. Any time you have a data structure of variable size, things can get -->
7
+ <!-- tricky, and strings are a re-sizable data structure. That being said, Rust’s -->
8
+ <!-- strings also work differently than in some other systems languages, such as C. -->
9
+ 文字列は、プログラマーがマスターすべき重要なコンセプトです。
10
+ Rustの文字列の扱いは、Rust言語がシステムにフォーカスしているため、少し他の言語と異なります。
11
+ 動的なサイズを持つデータ構造が有る時、常に物事は複雑になります、そして文字列もまたサイズを変更することができるデータ構造です。
12
+ これはつまり、Rustの文字列もまた、Cのような他のシステム言語とは少し異なるということです。
13
+
14
+
15
+ <!-- Let’s dig into the details. A ‘string’ is a sequence of Unicode scalar values -->
16
+ <!-- encoded as a stream of UTF-8 bytes. All strings are guaranteed to be a valid -->
17
+ <!-- encoding of UTF-8 sequences. Additionally, unlike some systems languages, -->
18
+ <!-- strings are not null-terminated and can contain null bytes. -->
19
+ 詳しく見ていきましょう、 「文字列」は、UTF-8のバイトストリームとしてエンコードされたユニコードのスカラー値のシーケンスです。
20
+ すべての文字列は、正しくエンコードされたUTF-8のシーケンスであることが保証されています。
21
+ また、他のシステム言語とはことなり、文字列はnull終端でなく、nullバイトを含むことが可能です。
22
+
23
+ <!-- Rust has two main types of strings: `&str` and `String`. Let’s talk about -->
24
+ <!-- `&str` first. These are called ‘string slices’. A string slice has a fixed -->
25
+ <!-- size, and cannot be mutated. It is a reference to a sequence of UTF-8 bytes. -->
26
+ Rustは主要な文字列型を二種類持っています: ` &str ` と ` String ` です。
27
+ まず ` &str ` について説明しましょう。 ` &str ` は「文字列のスライス」と呼ばれます。
28
+ 文字列のスライスは固定のサイズを持ち、変更不可能です。そして、文字列のスライスはUTF-8のバイトシーケンスへの参照です。
2
29
3
- Strings are an important concept for any programmer to master. Rust’s string
4
- handling system is a bit different from other languages, due to its systems
5
- focus. Any time you have a data structure of variable size, things can get
6
- tricky, and strings are a re-sizable data structure. That being said, Rust’s
7
- strings also work differently than in some other systems languages, such as C.
8
-
9
- Let’s dig into the details. A ‘string’ is a sequence of Unicode scalar values
10
- encoded as a stream of UTF-8 bytes. All strings are guaranteed to be a valid
11
- encoding of UTF-8 sequences. Additionally, unlike some systems languages,
12
- strings are not null-terminated and can contain null bytes.
13
-
14
- Rust has two main types of strings: ` &str ` and ` String ` . Let’s talk about
15
- ` &str ` first. These are called ‘string slices’. A string slice has a fixed
16
- size, and cannot be mutated. It is a reference to a sequence of UTF-8 bytes.
17
30
18
31
``` rust
19
32
let greeting = " Hello there." ; // greeting: &'static str
20
33
```
21
34
22
- ` "Hello there." ` is a string literal and its type is ` &'static str ` . A string
23
- literal is a string slice that is statically allocated, meaning that it’s saved
24
- inside our compiled program, and exists for the entire duration it runs. The
25
- ` greeting ` binding is a reference to this statically allocated string. Any
26
- function expecting a string slice will also accept a string literal.
27
-
28
- String literals can span multiple lines. There are two forms. The first will
29
- include the newline and the leading spaces:
35
+ <!-- `"Hello there."` is a string literal and its type is `&'static str`. A string -->
36
+ <!-- literal is a string slice that is statically allocated, meaning that it’s saved -->
37
+ <!-- inside our compiled program, and exists for the entire duration it runs. The -->
38
+ <!-- `greeting` binding is a reference to this statically allocated string. Any -->
39
+ <!-- function expecting a string slice will also accept a string literal. -->
40
+ ` "Hello there." ` は文字列リテラルであり、 ` &'static str ` 型を持ちます。
41
+ 文字列リテラルは、静的にアロケートされた文字列のスライスであり、これはつまりコンパイルされたプログラム中に保存されており、
42
+ プログラムの実行中全てにわたって存在していることを意味しています。
43
+ どの文字列のスライスを引数として期待している関数も、文字列リテラルを引数に取ることができます。
44
+
45
+ <!-- String literals can span multiple lines. There are two forms. The first will -->
46
+ <!-- include the newline and the leading spaces: -->
47
+ 文字列リテラルは複数行に渡ることができます。
48
+ 複数行の文字列リテラルを記述する方法は、2つの形式があります。
49
+ 一つ目の形式は、改行と行頭の空白を文字列に含む形式です:
30
50
31
51
``` rust
32
52
let s = " foo
@@ -35,19 +55,23 @@ let s = "foo
35
55
assert_eq! (" foo\ n bar" , s );
36
56
```
37
57
38
- The second, with a ` \ ` , trims the spaces and the newline:
58
+ <!-- The second, with a `\`, trims the spaces and the newline: -->
59
+ もう一つは ` \ ` を用いて、空白と改行を削る形式です:
39
60
40
61
``` rust
41
62
let s = " foo\
42
- bar" ;
63
+ bar" ;
43
64
44
65
assert_eq! (" foobar" , s );
45
66
```
46
67
47
- Rust has more than just ` &str ` s though. A ` String ` , is a heap-allocated string.
48
- This string is growable, and is also guaranteed to be UTF-8. ` String ` s are
49
- commonly created by converting from a string slice using the ` to_string `
50
- method.
68
+ <!-- Rust has more than just `&str`s though. A `String`, is a heap-allocated string. -->
69
+ <!-- This string is growable, and is also guaranteed to be UTF-8. `String`s are -->
70
+ <!-- commonly created by converting from a string slice using the `to_string` -->
71
+ <!-- method. -->
72
+ Rustは ` &str ` だけでなく、 ` String ` というヒープにアロケートされる文字列の形式も持っています。
73
+ この文字列は伸張可能であり、またUTF-8であることが保証されています。
74
+ ` String ` は一般的に文字列のスライスをメソッド ` to_string ` を用いて変換することで生成されます。
51
75
52
76
``` rust
53
77
let mut s = " Hello" . to_string (); // mut s: String
@@ -57,7 +81,8 @@ s.push_str(", world.");
57
81
println! (" {}" , s );
58
82
```
59
83
60
- ` String ` s will coerce into ` &str ` with an ` & ` :
84
+ <!-- `String`s will coerce into `&str` with an `&`: -->
85
+ ` String ` は ` & ` によって ` &str ` に型強制されます。
61
86
62
87
``` rust
63
88
fn takes_slice (slice : & str ) {
@@ -70,39 +95,55 @@ fn main() {
70
95
}
71
96
```
72
97
73
- This coercion does not happen for functions that accept one of ` &str ` ’s traits
74
- instead of ` &str ` . For example, [ ` TcpStream::connect ` ] [ connect ] has a parameter
75
- of type ` ToSocketAddrs ` . A ` &str ` is okay but a ` String ` must be explicitly
76
- converted using ` &* ` .
98
+ <!-- This coercion does not happen for functions that accept one of `&str`’s traits -->
99
+ <!-- instead of `&str`. For example, [`TcpStream::connect`][connect] has a parameter -->
100
+ <!-- of type `ToSocketAddrs`. A `&str` is okay but a `String` must be explicitly -->
101
+ <!-- converted using `&*`. -->
102
+ このような変換は ` &str ` の代わりに、 ` &str ` のトレイトを引数として期待している関数では自動的には行われません。
103
+ たとえば、 [ ` TcpStream::connect ` ] [ connect ] は引数として型 ` ToSocketAddrs ` を要求しています。
104
+ このような関数には ` &str ` は渡せますが、 ` String ` は ` &* ` を用いて明示的に変換する必要があります。
77
105
78
106
``` rust,no_run
79
107
use std::net::TcpStream;
80
108
81
- TcpStream::connect("192.168.0.1:3000"); // &str parameter
109
+ # // TcpStream::connect("192.168.0.1:3000"); // &str parameter
110
+ TcpStream::connect("192.168.0.1:3000"); // 引数として &str を渡す
82
111
83
112
let addr_string = "192.168.0.1:3000".to_string();
84
- TcpStream::connect(&*addr_string); // convert addr_string to &str
113
+ # // TcpStream::connect(&*addr_string); // convert addr_string to &str
114
+ TcpStream::connect(&*addr_string); // addr_string を &str に変換して渡す
85
115
```
86
116
87
- Viewing a ` String ` as a ` &str ` is cheap, but converting the ` &str ` to a
88
- ` String ` involves allocating memory. No reason to do that unless you have to!
117
+ <!-- Viewing a `String` as a `&str` is cheap, but converting the `&str` to a -->
118
+ <!-- `String` involves allocating memory. No reason to do that unless you have to! -->
119
+ ` String ` を ` &str ` として見るコストは低いですが、` &str ` を ` String ` に変換するのはメモリのアロケーションを発生させます。
120
+ 必要がなければ、やるべきではないでしょう!
89
121
90
- ## Indexing
122
+ <!-- ## Indexing -->
123
+ ## インデクシング
91
124
92
- Because strings are valid UTF-8, strings do not support indexing:
125
+ <!-- Because strings are valid UTF-8, strings do not support indexing: -->
126
+ 文字列が正しいUTF-8であるため、文字列はインデクシングをサポートしていません:
93
127
94
128
``` rust,ignore
95
129
let s = "hello";
96
130
97
- println!("The first letter of s is {}", s[0]); // ERROR!!!
131
+ # // println!("The first letter of s is {}", s[0]); // ERROR!!!
132
+ println!("The first letter of s is {}", s[0]); // エラー!!!
98
133
```
99
134
100
- Usually, access to a vector with ` [] ` is very fast. But, because each character
101
- in a UTF-8 encoded string can be multiple bytes, you have to walk over the
102
- string to find the nᵗʰ letter of a string. This is a significantly more
103
- expensive operation, and we don’t want to be misleading. Furthermore, ‘letter’
104
- isn’t something defined in Unicode, exactly. We can choose to look at a string as
105
- individual bytes, or as codepoints:
135
+ <!-- Usually, access to a vector with `[]` is very fast. But, because each character -->
136
+ <!-- in a UTF-8 encoded string can be multiple bytes, you have to walk over the -->
137
+ <!-- string to find the nᵗʰ letter of a string. This is a significantly more -->
138
+ <!-- expensive operation, and we don’t want to be misleading. Furthermore, ‘letter’ -->
139
+ <!-- isn’t something defined in Unicode, exactly. We can choose to look at a string as -->
140
+ <!-- individual bytes, or as codepoints:-->
141
+ 普通、ベクターへの ` [] ` を用いたアクセスはとても高速です。
142
+ しかし、UTF-8にエンコードされた文字列中の一つ一つの文字は複数のバイトであることが可能なため、
143
+ 文字列のn番の文字を探すためには文字列上を移動していく必要があります。
144
+ そのような作業はとても高コストな演算であり、誤解してはならない点です。
145
+ さらに言えば、「文字」というものはUnicodeにおいては正確に定義されていません。
146
+ 文字列を、それぞれのバイトとして見ることも、コードポインの集まりとして見ることもできるのです。
106
147
107
148
``` rust
108
149
let hachiko = " 忠犬ハチ公" ;
@@ -120,51 +161,62 @@ for c in hachiko.chars() {
120
161
println! ("" );
121
162
```
122
163
123
- This prints:
164
+ <!-- This prints: -->
165
+ これは、以下の様な出力をします:
124
166
125
167
``` text
126
168
229, 191, 160, 231, 138, 172, 227, 131, 143, 227, 131, 129, 229, 133, 172,
127
169
忠, 犬, ハ, チ, 公,
128
170
```
129
171
130
- As you can see, there are more bytes than ` char ` s.
172
+ <!-- As you can see, there are more bytes than `char`s.-->
173
+ ご覧のように、 ` char ` の数よりも多くのバイトが含まれています。
131
174
132
- You can get something similar to an index like this:
175
+ <!-- You can get something similar to an index like this: -->
176
+ インデクシングするのと近い結果を以下の様にして得ることができます:
133
177
134
178
``` rust
135
179
# let hachiko = " 忠犬ハチ公" ;
136
180
let dog = hachiko . chars (). nth (1 ); // kinda like hachiko[1]
137
181
```
138
182
139
- This emphasizes that we have to walk from the beginning of the list of ` chars ` .
183
+ <!-- This emphasizes that we have to walk from the beginning of the list of `chars`. -->
184
+ このコードは、` chars ` のリストの上を移動している事を強調しています。
140
185
141
- ## Slicing
186
+ ## スライシング
142
187
143
- You can get a slice of a string with slicing syntax:
188
+ <!-- You can get a slice of a string with slicing syntax: -->
189
+ 文字列のスライスは以下のようにスライス構文を用いて取得することができます:
144
190
145
191
``` rust
146
192
let dog = " hachiko" ;
147
193
let hachi = & dog [0 .. 5 ];
148
194
```
149
195
150
- But note that these are _ byte_ offsets, not _ character_ offsets. So
151
- this will fail at runtime:
196
+ <!-- But note that these are _byte_ offsets, not _character_ offsets. So -->
197
+ <!-- this will fail at runtime: -->
198
+ しかし、注意しなくてはならない点はこれらのオフセットは _ バイト_ であって _ 文字_ のオフセットではないという点です。
199
+ そのため、以下のコードは実行時に失敗します:
152
200
153
201
``` rust,should_panic
154
202
let dog = "忠犬ハチ公";
155
203
let hachi = &dog[0..2];
156
204
```
157
205
158
- with this error:
206
+ <!-- with this error: -->
207
+ そして、次のようなエラーが発生します:
208
+
159
209
160
210
``` text
161
211
thread '<main>' panicked at 'index 0 and/or 2 in `忠犬ハチ公` do not lie on
162
212
character boundary'
163
213
```
164
214
165
- ## Concatenation
215
+ <!-- ## Concatenation -->
216
+ ## 結合
166
217
167
- If you have a ` String ` , you can concatenate a ` &str ` to the end of it:
218
+ <!-- If you have a `String`, you can concatenate a `&str` to the end of it: -->
219
+ ` String ` が存在するとき、 ` &str ` を末尾に結合することができます:
168
220
169
221
``` rust
170
222
let hello = " Hello " . to_string ();
@@ -173,7 +225,8 @@ let world = "world!";
173
225
let hello_world = hello + world ;
174
226
```
175
227
176
- But if you have two ` String ` s, you need an ` & ` :
228
+ <!-- But if you have two `String`s, you need an `&`: -->
229
+ しかし、2つの ` String ` を結合するには、 ` & ` が必要になります:
177
230
178
231
``` rust
179
232
let hello = " Hello " . to_string ();
@@ -182,8 +235,10 @@ let world = "world!".to_string();
182
235
let hello_world = hello + & world ;
183
236
```
184
237
185
- This is because ` &String ` can automatically coerce to a ` &str ` . This is a
186
- feature called ‘[ ` Deref ` coercions] [ dc ] ’.
238
+ <!-- This is because `&String` can automatically coerce to a `&str`. This is a -->
239
+ <!-- feature called ‘[`Deref` coercions][dc]’. -->
240
+ これは、 ` &String ` が自動的に ` &str ` に型強制されるためです。
241
+ このフィーチャーは 「 [ ` Deref ` による型強制] [ dc ] 」と呼ばれています。
187
242
188
243
[ dc ] : deref-coercions.html
189
244
[ connect ] : ../std/net/struct.TcpStream.html#method.connect
0 commit comments