moved value: `s` --> src/main.rs:4:20 | 2 | let s = String::new(); | - move occurs because `s` has type `String`, which does not implement the `Copy` trait 3 | let p = s; | - value moved here 4 | println!("{}", s); | ^ value borrowed here after move |
not live long enough --> src/main.rs:5:13 | 5 | a = &b; | ^^ borrowed value does not live long enough 6 | } //v is dropped here | - `b` dropped here while still borrowed 7 | a.len(); //error:borrowed value does not live long enough | ------- borrow later used here
let mut a = String::from("hello"); let b = &mut a; // 1つ目のミュータブルな参照 let c = &mut a; // 2つ目のミュータブルな参照 b.push_str("a"); // cannot borrow `a` as mutable more than once at a time } ミュータブルな参照は一度に一つだけ。
let string1 = String::from("abcd"); let string2 = "xyz"; let result = longest(string1.as_str(), string2); println!("The longest string is {}", result); } 長い方のStringを返すような関数を書いたとする。
lifetime specifier --> src/main.rs:9:33 | 9 | fn longest(x: &str, y: &str) -> &str { | ---- ---- ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `x` or `y` help: consider introducing a named lifetime parameter | 9 | fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { | ++++ ++ ++ ++
specifier --> src/main.rs:9:33 | 9 | fn longest(x: &str, y: &str) -> &str { | ---- ---- ^ expected named lifetime parameter | = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `x` or `y` help: consider introducing a named lifetime parameter | 9 | fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { | ++++ ++ ++ ++
does not live long enough --> src/main.rs:6:44 | 6 | result = longest(string1.as_str(), string2.as_str()); | ^^^^^^^^^^^^^^^^ borrowed value does not live long enough 7 | } | - `string2` dropped here while still borrowed 8 | println!("The longest string is {}", result); | ------ borrow later used here
joy of Rust https://hashrust.com • Validating References with Lifetimes https://doc.rust-lang.org/book/ch10-03-lifetime-syntax.html • constants - Rust By Example https://doc.rust-lang.org/rust-by-example/custom_types/constants.html • A command-line shell like fish, but POSIX compatible. https://github.com/nuta/nsh