Skip to content

Commit 005ab57

Browse files
committed
ch04 所有権を理解するの和訳を最新版に更新
rust-lang/book@19c40bf
1 parent 49f93be commit 005ab57

File tree

51 files changed

+749
-1045
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+749
-1045
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "ownership"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
fn main() {
22
// ANCHOR: here
3-
{ // s is not valid here, it’s not yet declared
4-
let s = "hello"; // s is valid from this point forward
3+
{ // sは、ここでは有効ではない。まだ宣言されていない
4+
let s = "hello"; // sは、ここから有効になる
55

6-
// do stuff with s
7-
} // this scope is now over, and s is no longer valid
6+
// sで作業をする
7+
} // このスコープは終わり。もうsは有効ではない
88
// ANCHOR_END: here
99
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "ownership"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "ownership"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
fn main() {
2-
let s = String::from("hello"); // s comes into scope
2+
let s = String::from("hello"); // sがスコープに入る
33

4-
takes_ownership(s); // s's value moves into the function...
5-
// ... and so is no longer valid here
4+
takes_ownership(s); // sの値が関数にムーブされ...
5+
// ... ここではもう有効ではない
66

7-
let x = 5; // x comes into scope
7+
let x = 5; // xがスコープに入る
88

9-
makes_copy(x); // x would move into the function,
10-
// but i32 is Copy, so it’s okay to still
11-
// use x afterward
9+
makes_copy(x); // xも関数にムーブされるが、
10+
// i32はCopyなので、この後にxを使っても
11+
// 大丈夫
1212

13-
} // Here, x goes out of scope, then s. But because s's value was moved, nothing
14-
// special happens.
13+
} // ここでxがスコープを抜け、sもスコープを抜ける。ただし、sの値はムーブされているので、
14+
// 何も特別なことは起こらない。
1515

16-
fn takes_ownership(some_string: String) { // some_string comes into scope
16+
fn takes_ownership(some_string: String) { // some_stringがスコープに入る。
1717
println!("{}", some_string);
18-
} // Here, some_string goes out of scope and `drop` is called. The backing
19-
// memory is freed.
18+
} // ここでsome_stringがスコープを抜け、`drop`が呼ばれる。後ろ盾してたメモリが解放される。
19+
// 後ろ盾してたメモリが解放される。
2020

21-
fn makes_copy(some_integer: i32) { // some_integer comes into scope
21+
fn makes_copy(some_integer: i32) { // some_integerがスコープに入る
2222
println!("{}", some_integer);
23-
} // Here, some_integer goes out of scope. Nothing special happens.
23+
} // ここでsome_integerがスコープを抜ける。何も特別なことはない。
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "ownership"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
fn main() {
2-
let s1 = gives_ownership(); // gives_ownership moves its return
3-
// value into s1
2+
let s1 = gives_ownership(); // gives_ownershipは、戻り値をs1に
3+
// ムーブする
44

5-
let s2 = String::from("hello"); // s2 comes into scope
5+
let s2 = String::from("hello"); // s2がスコープに入る
66

7-
let s3 = takes_and_gives_back(s2); // s2 is moved into
8-
// takes_and_gives_back, which also
9-
// moves its return value into s3
10-
} // Here, s3 goes out of scope and is dropped. s2 goes out of scope but was
11-
// moved, so nothing happens. s1 goes out of scope and is dropped.
7+
let s3 = takes_and_gives_back(s2); // s2はtakes_and_gives_backにムーブされ
8+
// 戻り値もs3にムーブされる
9+
} // ここで、s3はスコープを抜け、ドロップされる。s2もスコープを抜けるが、ムーブされているので、
10+
// 何も起きない。s1もスコープを抜け、ドロップされる。
1211

13-
fn gives_ownership() -> String { // gives_ownership will move its
14-
// return value into the function
15-
// that calls it
12+
fn gives_ownership() -> String { // gives_ownershipは、戻り値を
13+
// 呼び出した関数にムーブする
1614

17-
let some_string = String::from("hello"); // some_string comes into scope
15+
let some_string = String::from("hello"); // some_stringがスコープに入る
1816

19-
some_string // some_string is returned and
20-
// moves out to the calling
21-
// function
17+
some_string // some_stringが返され、呼び出し元関数に
18+
// ムーブされる
2219
}
2320

24-
// takes_and_gives_back will take a String and return one
25-
fn takes_and_gives_back(a_string: String) -> String { // a_string comes into
26-
// scope
21+
// takes_and_gives_backは、Stringを一つ受け取り、返す。
22+
fn takes_and_gives_back(a_string: String) -> String { // a_stringがスコープに入る。
2723

28-
a_string // a_string is returned and moves out to the calling function
24+
a_string // a_stringが返され、呼び出し元関数にムーブされる
2925
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "ownership"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch04-understanding-ownership/listing-04-05/src/main.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ fn main() {
33

44
let (s2, len) = calculate_length(s1);
55

6+
//'{}'の長さは、{}です
67
println!("The length of '{}' is {}.", s2, len);
78
}
89

910
fn calculate_length(s: String) -> (String, usize) {
10-
let length = s.len(); // len() returns the length of a String
11+
let length = s.len(); // len()メソッドは、Stringの長さを返します
1112

1213
(s, length)
1314
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "ownership"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
$ cargo run
22
Compiling ownership v0.1.0 (file:///projects/ownership)
33
error[E0596]: cannot borrow `*some_string` as mutable, as it is behind a `&` reference
4+
(`*some_string`は`&`参照の背後にあるため、それを可変として借用することはできません)
45
--> src/main.rs:8:5
56
|
6-
7 | fn change(some_string: &String) {
7-
| ------- help: consider changing this to be a mutable reference: `&mut std::string::String`
87
8 | some_string.push_str(", world");
98
| ^^^^^^^^^^^ `some_string` is a `&` reference, so the data it refers to cannot be borrowed as mutable
10-
11-
error: aborting due to previous error
9+
| (`some_string`は`&`参照なので、それが指すデータを可変として借用することはできません)
10+
|
11+
help: consider changing this to be a mutable reference
12+
(これを可変参照に変更することを検討してください)
13+
|
14+
7 | fn change(some_string: &mut String) {
15+
| +++
1216

1317
For more information about this error, try `rustc --explain E0596`.
14-
error: could not compile `ownership`.
15-
16-
To learn more, run the command again with --verbose.
18+
error: could not compile `ownership` (bin "ownership") due to 1 previous error
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "ownership"
33
version = "0.1.0"
4-
authors = ["Carol (Nichols || Goulding) <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "ownership"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "ownership"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch04-understanding-ownership/listing-04-09/src/main.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@ fn first_word(s: &str) -> &str {
1616
fn main() {
1717
let my_string = String::from("hello world");
1818

19-
// first_word works on slices of `String`s
20-
// first_wordは`String`のスライスに対して機能する
19+
// `first_word` works on slices of `String`s, whether partial or whole
20+
// `first_word`は`String`の全体または部分へのスライスに対して機能する
21+
let word = first_word(&my_string[0..6]);
2122
let word = first_word(&my_string[..]);
23+
// `first_word` also works on references to `String`s, which are equivalent
24+
// to whole slices of `String`s
25+
// `first_word`は`String`の参照に対しても機能する。この場合は
26+
// `String`全体へのスライスと同等。
27+
let word = first_word(&my_string);
2228

2329
let my_string_literal = "hello world";
2430

25-
// first_word works on slices of string literals
26-
// first_wordは文字列リテラルのスライスに対して機能する
31+
// `first_word` works on slices of string literals, whether partial or whole
32+
// `first_word`は文字列リテラルの全体または部分へのスライスに対して機能する
33+
let word = first_word(&my_string_literal[0..6]);
2734
let word = first_word(&my_string_literal[..]);
2835

2936
// Because string literals *are* string slices already,
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "ownership"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch04-understanding-ownership/no-listing-01-can-mutate-string/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ fn main() {
22
// ANCHOR: here
33
let mut s = String::from("hello");
44

5-
s.push_str(", world!"); // push_str() appends a literal to a String
5+
s.push_str(", world!"); // push_str()関数は、リテラルをStringに付け加える
66

7-
println!("{}", s); // This will print `hello, world!`
7+
println!("{}", s); // これは`hello, world!`と出力する
88
// ANCHOR_END: here
99
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "ownership"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
fn main() {
22
// ANCHOR: here
3-
{
4-
let s = String::from("hello"); // s is valid from this point forward
3+
{
4+
let s = String::from("hello"); // sはここから有効になる
55

6-
// do stuff with s
7-
} // this scope is now over, and s is no
8-
// longer valid
6+
// sで作業をする
7+
} // このスコープはここでおしまい。sは
8+
// もう有効ではない
99
// ANCHOR_END: here
1010
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "ownership"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "ownership"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
$ cargo run
22
Compiling ownership v0.1.0 (file:///projects/ownership)
33
error[E0382]: borrow of moved value: `s1`
4+
(ムーブされた値の借用: `s1`)
45
--> src/main.rs:5:28
56
|
67
2 | let s1 = String::from("hello");
7-
| -- move occurs because `s1` has type `std::string::String`, which does not implement the `Copy` trait
8+
| -- move occurs because `s1` has type `String`, which does not implement the `Copy` trait
9+
| (`s1`は`String`型を持ち、これは`Copy`トレイトを実装していないので、ムーブが発生します)
810
3 | let s2 = s1;
911
| -- value moved here
10-
4 |
12+
| (ここで値がムーブされました)
13+
4 |
1114
5 | println!("{}, world!", s1);
1215
| ^^ value borrowed here after move
13-
14-
error: aborting due to previous error
16+
| (ムーブ後にここで借用されています)
17+
|
18+
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
19+
help: consider cloning the value if the performance cost is acceptable
20+
|
21+
3 | let s2 = s1.clone();
22+
| ++++++++
1523

1624
For more information about this error, try `rustc --explain E0382`.
17-
error: could not compile `ownership`.
18-
19-
To learn more, run the command again with --verbose.
25+
error: could not compile `ownership` (bin "ownership") due to 1 previous error
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "ownership"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "ownership"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "ownership"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch04-understanding-ownership/no-listing-07-reference/src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ fn main() {
66
let len = calculate_length(&s1);
77
// ANCHOR_END: here
88

9+
// '{}'の長さは、{}です
910
println!("The length of '{}' is {}.", s1, len);
1011
}
1112

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
22
name = "ownership"
33
version = "0.1.0"
4-
authors = ["Your Name <[email protected]>"]
5-
edition = "2018"
4+
edition = "2021"
65

76
[dependencies]

listings/ch04-understanding-ownership/no-listing-08-reference-with-annotations/src/main.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ fn main() {
77
}
88

99
// ANCHOR: here
10-
fn calculate_length(s: &String) -> usize { // s is a reference to a String
10+
fn calculate_length(s: &String) -> usize { // sはStringへの参照
1111
s.len()
12-
} // Here, s goes out of scope. But because it does not have ownership of what
13-
// it refers to, nothing happens.
12+
} // ここで、sはスコープ外になる。けど、参照しているものの所有権を持っているわけではないので
13+
// 何も起こらない
1414
// ANCHOR_END: here

0 commit comments

Comments
 (0)