January 2018
Beginner to intermediate
454 pages
10h 8m
English
Let's try the following code, which would work in other programming languages:
let p1 = Point { x: 1, y: 2 }; let p2 = p1; println!("{}", p1.x);
We can see that Rust doesn't accept this. It gives the following error:
error[E0382]: use of moved value: `p1.x`
--> src/main.rs:4:20
|
3 | let p2 = p1;
| -- value moved here
4 | println!("{}", p1.x);
| ^^^^ value used here after move
|
= note: move occurs because `p1` has type `Point`, which does not implement the `Copy` trait
This means that we cannot use a value after it is moved. In Rust, values are moved by default instead of being copied, except in some cases, as we'll see in the next sub-section.
To avoid moving a value, we can take a reference to it by prefixing it with &:
let ...Read now
Unlock full access