Skip to content

Commit 403d47b

Browse files
committed
Merge pull request #69 from pocket7878/string
4.17. String
2 parents fb41721 + 474e71a commit 403d47b

File tree

2 files changed

+117
-61
lines changed

2 files changed

+117
-61
lines changed

1.6/ja/book/strings.md

Lines changed: 116 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,52 @@
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のバイトシーケンスへの参照です。
229

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.
1730

1831
```rust
1932
let greeting = "Hello there."; // greeting: &'static str
2033
```
2134

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+
一つ目の形式は、改行と行頭の空白を文字列に含む形式です:
3050

3151
```rust
3252
let s = "foo
@@ -35,19 +55,23 @@ let s = "foo
3555
assert_eq!("foo\n bar", s);
3656
```
3757

38-
The second, with a `\`, trims the spaces and the newline:
58+
<!-- The second, with a `\`, trims the spaces and the newline: -->
59+
もう一つは `\` を用いて、空白と改行を削る形式です:
3960

4061
```rust
4162
let s = "foo\
42-
bar";
63+
bar";
4364

4465
assert_eq!("foobar", s);
4566
```
4667

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` を用いて変換することで生成されます。
5175

5276
```rust
5377
let mut s = "Hello".to_string(); // mut s: String
@@ -57,7 +81,8 @@ s.push_str(", world.");
5781
println!("{}", s);
5882
```
5983

60-
`String`s will coerce into `&str` with an `&`:
84+
<!-- `String`s will coerce into `&str` with an `&`: -->
85+
`String``&` によって `&str` に型強制されます。
6186

6287
```rust
6388
fn takes_slice(slice: &str) {
@@ -70,39 +95,55 @@ fn main() {
7095
}
7196
```
7297

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``&*` を用いて明示的に変換する必要があります。
77105

78106
```rust,no_run
79107
use std::net::TcpStream;
80108
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 を渡す
82111
83112
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 に変換して渡す
85115
```
86116

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+
必要がなければ、やるべきではないでしょう!
89121

90-
## Indexing
122+
<!-- ## Indexing -->
123+
## インデクシング
91124

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であるため、文字列はインデクシングをサポートしていません:
93127

94128
```rust,ignore
95129
let s = "hello";
96130
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]); // エラー!!!
98133
```
99134

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+
文字列を、それぞれのバイトとして見ることも、コードポインの集まりとして見ることもできるのです。
106147

107148
```rust
108149
let hachiko = "忠犬ハチ公";
@@ -120,51 +161,62 @@ for c in hachiko.chars() {
120161
println!("");
121162
```
122163

123-
This prints:
164+
<!-- This prints: -->
165+
これは、以下の様な出力をします:
124166

125167
```text
126168
229, 191, 160, 231, 138, 172, 227, 131, 143, 227, 131, 129, 229, 133, 172,
127169
忠, 犬, ハ, チ, 公,
128170
```
129171

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` の数よりも多くのバイトが含まれています。
131174

132-
You can get something similar to an index like this:
175+
<!-- You can get something similar to an index like this: -->
176+
インデクシングするのと近い結果を以下の様にして得ることができます:
133177

134178
```rust
135179
# let hachiko = "忠犬ハチ公";
136180
let dog = hachiko.chars().nth(1); // kinda like hachiko[1]
137181
```
138182

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` のリストの上を移動している事を強調しています。
140185

141-
## Slicing
186+
## スライシング
142187

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+
文字列のスライスは以下のようにスライス構文を用いて取得することができます:
144190

145191
```rust
146192
let dog = "hachiko";
147193
let hachi = &dog[0..5];
148194
```
149195

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+
そのため、以下のコードは実行時に失敗します:
152200

153201
```rust,should_panic
154202
let dog = "忠犬ハチ公";
155203
let hachi = &dog[0..2];
156204
```
157205

158-
with this error:
206+
<!-- with this error: -->
207+
そして、次のようなエラーが発生します:
208+
159209

160210
```text
161211
thread '<main>' panicked at 'index 0 and/or 2 in `忠犬ハチ公` do not lie on
162212
character boundary'
163213
```
164214

165-
## Concatenation
215+
<!-- ## Concatenation -->
216+
## 結合
166217

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` を末尾に結合することができます:
168220

169221
```rust
170222
let hello = "Hello ".to_string();
@@ -173,7 +225,8 @@ let world = "world!";
173225
let hello_world = hello + world;
174226
```
175227

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` を結合するには、 `&` が必要になります:
177230

178231
```rust
179232
let hello = "Hello ".to_string();
@@ -182,8 +235,10 @@ let world = "world!".to_string();
182235
let hello_world = hello + &world;
183236
```
184237

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] 」と呼ばれています。
187242

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

TranslationTable.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
| expression statement | 式文
4444
| feature | フィーチャ
4545
| generics | ジェネリクス
46+
| growable | 伸張可能
4647
| identifier | 識別子
4748
| immutable | イミュータブル
4849
| keyword | キーワード

0 commit comments

Comments
 (0)