Skip to content

Commit fa483c1

Browse files
authored
Merge pull request #97 from woodyZootopia/update_04-03
Update 04-03
2 parents 15dbb10 + a7725e7 commit fa483c1

File tree

5 files changed

+74
-234
lines changed

5 files changed

+74
-234
lines changed

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

+4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ fn main() {
1515
let mut s = String::from("hello world");
1616

1717
let word = first_word(&s); // word will get the value 5
18+
// wordの中身は、値5になる
1819

1920
s.clear(); // this empties the String, making it equal to ""
21+
// Stringを空にする。つまり、""と等しくする
2022

2123
// word still has the value 5 here, but there's no more string that
2224
// we could meaningfully use the value 5 with. word is now totally invalid!
25+
// wordはまだ値5を保持しているが、もうこの値を正しい意味で使用できる文字列は存在しない。
26+
// wordは今や完全に無効なのだ!
2327
}
2428
// ANCHOR_END: here

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

+4
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@ fn main() {
1717
let my_string = String::from("hello world");
1818

1919
// first_word works on slices of `String`s
20+
// first_wordは`String`のスライスに対して機能する
2021
let word = first_word(&my_string[..]);
2122

2223
let my_string_literal = "hello world";
2324

2425
// first_word works on slices of string literals
26+
// first_wordは文字列リテラルのスライスに対して機能する
2527
let word = first_word(&my_string_literal[..]);
2628

2729
// Because string literals *are* string slices already,
2830
// this works too, without the slice syntax!
31+
// 文字列リテラルは「それ自体すでに文字列スライスなので」、
32+
// スライス記法なしでも機能するのだ!
2933
let word = first_word(my_string_literal);
3034
}
3135
// ANCHOR_END: usage

listings/ch04-understanding-ownership/no-listing-19-slice-error/output.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
$ cargo run
22
Compiling ownership v0.1.0 (file:///projects/ownership)
33
error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable
4+
(エラー: 不変として借用されているので、`s`を可変で借用できません)
45
--> src/main.rs:18:5
56
|
67
16 | let word = first_word(&s);
78
| -- immutable borrow occurs here
9+
| (不変借用はここで発生しています)
810
17 |
911
18 | s.clear(); // error!
1012
| ^^^^^^^^^ mutable borrow occurs here
13+
| (可変借用はここで発生しています)
1114
19 |
1215
20 | println!("the first word is: {}", word);
1316
| ---- immutable borrow later used here
17+
(不変借用はその後ここで使われています)
1418

1519
error: aborting due to previous error
1620

listings/ch04-understanding-ownership/no-listing-19-slice-error/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn main() {
1616

1717
let word = first_word(&s);
1818

19-
s.clear(); // error!
19+
s.clear(); // error! (エラー!)
2020

2121
println!("the first word is: {}", word);
2222
}

0 commit comments

Comments
 (0)