fn main() {
{
let x = 1;
let y = x;
println!("x: {}", x);
println!("y: {}", y);
}
}
output
x: 1
y: 1
rust
fn main() {
{
let x = String::from("hello");
let y = x;
println!("x: {}", x); // NG
println!("y: {}", y);
}
}
error
error[E0382]: borrow of moved value: `x`
--> src/main.rs:5:27
|
3 | let x = String::from("hello");
| - move occurs because `x` has type `String`, which does not implement the `Copy` trait
4 | let y = x;
| - value moved here
5 | println!("x: {}", x);
| ^ value borrowed here after move
|
= 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)
help: consider cloning the value if the performance cost is acceptable
|
4 | let y = x.clone();
| ++++++++
C++だと
cpp
#include <iostream>
#include <string>
int main () {
std::string x("abc");
std::string y;
y = x; // (*) copy
std::cout << x << std::endl; // OK
std::cout << y << std::endl;
}
cpp
#include <iostream>
#include <string>
#include <memory>
int main () {
std::unique_ptr<std::string> x = std::make_unique<std::string>(std::string("abc"));
std::unique_ptr<std::string> y;
std::cout << *x << std::endl;
}
no member named 'make_unique' in namespace 'std'
make_unique is an upcoming C++14 feature
こうか
$clang++ -std=c++14 t.cpp && ./a.out
cpp
#include <iostream>
#include <string>
#include <memory>
int main () {
std::unique_ptr<std::string> x = std::make_unique<std::string>(std::string("abc"));
std::unique_ptr<std::string> y;
y = std::move(x);
std::cout << *y << std::endl;
std::cout << *x << std::endl; // here
}
fn main() {
{
let x = String::from("hello");
foo(x);
println!("x: {}", x); // NG
}
}
fn foo(x: String){
println!("x: {}", x);
}
error
error[E0382]: borrow of moved value: `x`
--> src/main.rs:5:27
|
3 | let x = String::from("hello");
| - move occurs because `x` has type `String`, which does not implement the `Copy` trait
4 | foo(x);
| - value moved here
5 | println!("x: {}", x); // NG
| ^ value borrowed here after move
|
note: consider changing this parameter type in function `foo` to borrow instead if owning the value isn't necessary
--> src/main.rs:8:11
|
8 | fn foo(x: String){
| --- ^^^^^^ this parameter takes ownership of the value
| |
| in this function
= 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)
help: consider cloning the value if the performance cost is acceptable
|
4 | foo(x.clone());
| ++++++++
cannot borrow s as mutable because it is also borrowed as immutable
rust
fn main() {
{
foo();
}
}
fn foo() -> &String{
let x = String::from("hello");
&x
}
error
error[E0106]: missing lifetime specifier
--> src/main.rs:6:13
|
6 | fn foo() -> &String{
| ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime
|
6 | fn foo() -> &'static String{
| +++++++