Skip to content

Update 04-03 #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ fn main() {
let mut s = String::from("hello world");

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

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

// word still has the value 5 here, but there's no more string that
// we could meaningfully use the value 5 with. word is now totally invalid!
// wordはまだ値5を保持しているが、もうこの値を正しい意味で使用できる文字列は存在しない。
// wordは今や完全に無効なのだ!
}
// ANCHOR_END: here
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,19 @@ fn main() {
let my_string = String::from("hello world");

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

let my_string_literal = "hello world";

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

// Because string literals *are* string slices already,
// this works too, without the slice syntax!
// 文字列リテラルは「それ自体すでに文字列スライスなので」、
// スライス記法なしでも機能するのだ!
let word = first_word(my_string_literal);
}
// ANCHOR_END: usage
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
$ cargo run
Compiling ownership v0.1.0 (file:///projects/ownership)
error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable
(エラー: 不変として借用されているので、`s`を可変で借用できません)
--> src/main.rs:18:5
|
16 | let word = first_word(&s);
| -- immutable borrow occurs here
| (不変借用はここで発生しています)
17 |
18 | s.clear(); // error!
| ^^^^^^^^^ mutable borrow occurs here
| (可変借用はここで発生しています)
19 |
20 | println!("the first word is: {}", word);
| ---- immutable borrow later used here
(不変借用はその後ここで使われています)

error: aborting due to previous error

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {

let word = first_word(&s);

s.clear(); // error!
s.clear(); // error! (エラー!)

println!("the first word is: {}", word);
}
Expand Down
Loading