File tree 5 files changed +74
-234
lines changed
listings/ch04-understanding-ownership
no-listing-19-slice-error
5 files changed +74
-234
lines changed Original file line number Diff line number Diff line change @@ -15,10 +15,14 @@ fn main() {
15
15
let mut s = String :: from ( "hello world" ) ;
16
16
17
17
let word = first_word ( & s) ; // word will get the value 5
18
+ // wordの中身は、値5になる
18
19
19
20
s. clear ( ) ; // this empties the String, making it equal to ""
21
+ // Stringを空にする。つまり、""と等しくする
20
22
21
23
// word still has the value 5 here, but there's no more string that
22
24
// we could meaningfully use the value 5 with. word is now totally invalid!
25
+ // wordはまだ値5を保持しているが、もうこの値を正しい意味で使用できる文字列は存在しない。
26
+ // wordは今や完全に無効なのだ!
23
27
}
24
28
// ANCHOR_END: here
Original file line number Diff line number Diff line change @@ -17,15 +17,19 @@ fn main() {
17
17
let my_string = String :: from ( "hello world" ) ;
18
18
19
19
// first_word works on slices of `String`s
20
+ // first_wordは`String`のスライスに対して機能する
20
21
let word = first_word ( & my_string[ ..] ) ;
21
22
22
23
let my_string_literal = "hello world" ;
23
24
24
25
// first_word works on slices of string literals
26
+ // first_wordは文字列リテラルのスライスに対して機能する
25
27
let word = first_word ( & my_string_literal[ ..] ) ;
26
28
27
29
// Because string literals *are* string slices already,
28
30
// this works too, without the slice syntax!
31
+ // 文字列リテラルは「それ自体すでに文字列スライスなので」、
32
+ // スライス記法なしでも機能するのだ!
29
33
let word = first_word ( my_string_literal) ;
30
34
}
31
35
// ANCHOR_END: usage
Original file line number Diff line number Diff line change 1
1
$ cargo run
2
2
Compiling ownership v0.1.0 (file:///projects/ownership)
3
3
error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable
4
+ (エラー: 不変として借用されているので、`s`を可変で借用できません)
4
5
--> src/main.rs:18:5
5
6
|
6
7
16 | let word = first_word(&s);
7
8
| -- immutable borrow occurs here
9
+ | (不変借用はここで発生しています)
8
10
17 |
9
11
18 | s.clear(); // error!
10
12
| ^^^^^^^^^ mutable borrow occurs here
13
+ | (可変借用はここで発生しています)
11
14
19 |
12
15
20 | println!("the first word is: {}", word);
13
16
| ---- immutable borrow later used here
17
+ (不変借用はその後ここで使われています)
14
18
15
19
error: aborting due to previous error
16
20
Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ fn main() {
16
16
17
17
let word = first_word ( & s) ;
18
18
19
- s. clear ( ) ; // error!
19
+ s. clear ( ) ; // error! (エラー!)
20
20
21
21
println ! ( "the first word is: {}" , word) ;
22
22
}
You can’t perform that action at this time.
0 commit comments