]> git.lizzy.rs Git - rust.git/blob - src/librustc_error_codes/error_codes/E0382.md
Rollup merge of #66377 - XAMPPRocky:relnotes-1.40.0, r=Mark-Simulacrum
[rust.git] / src / librustc_error_codes / error_codes / E0382.md
1 This error occurs when an attempt is made to use a variable after its contents
2 have been moved elsewhere.
3
4 Erroneous code example:
5
6 ```compile_fail,E0382
7 struct MyStruct { s: u32 }
8
9 fn main() {
10     let mut x = MyStruct{ s: 5u32 };
11     let y = x;
12     x.s = 6;
13     println!("{}", x.s);
14 }
15 ```
16
17 Since `MyStruct` is a type that is not marked `Copy`, the data gets moved out
18 of `x` when we set `y`. This is fundamental to Rust's ownership system: outside
19 of workarounds like `Rc`, a value cannot be owned by more than one variable.
20
21 Sometimes we don't need to move the value. Using a reference, we can let another
22 function borrow the value without changing its ownership. In the example below,
23 we don't actually have to move our string to `calculate_length`, we can give it
24 a reference to it with `&` instead.
25
26 ```
27 fn main() {
28     let s1 = String::from("hello");
29
30     let len = calculate_length(&s1);
31
32     println!("The length of '{}' is {}.", s1, len);
33 }
34
35 fn calculate_length(s: &String) -> usize {
36     s.len()
37 }
38 ```
39
40 A mutable reference can be created with `&mut`.
41
42 Sometimes we don't want a reference, but a duplicate. All types marked `Clone`
43 can be duplicated by calling `.clone()`. Subsequent changes to a clone do not
44 affect the original variable.
45
46 Most types in the standard library are marked `Clone`. The example below
47 demonstrates using `clone()` on a string. `s1` is first set to "many", and then
48 copied to `s2`. Then the first character of `s1` is removed, without affecting
49 `s2`. "any many" is printed to the console.
50
51 ```
52 fn main() {
53     let mut s1 = String::from("many");
54     let s2 = s1.clone();
55     s1.remove(0);
56     println!("{} {}", s1, s2);
57 }
58 ```
59
60 If we control the definition of a type, we can implement `Clone` on it ourselves
61 with `#[derive(Clone)]`.
62
63 Some types have no ownership semantics at all and are trivial to duplicate. An
64 example is `i32` and the other number types. We don't have to call `.clone()` to
65 clone them, because they are marked `Copy` in addition to `Clone`.  Implicit
66 cloning is more convenient in this case. We can mark our own types `Copy` if
67 all their members also are marked `Copy`.
68
69 In the example below, we implement a `Point` type. Because it only stores two
70 integers, we opt-out of ownership semantics with `Copy`. Then we can
71 `let p2 = p1` without `p1` being moved.
72
73 ```
74 #[derive(Copy, Clone)]
75 struct Point { x: i32, y: i32 }
76
77 fn main() {
78     let mut p1 = Point{ x: -1, y: 2 };
79     let p2 = p1;
80     p1.x = 1;
81     println!("p1: {}, {}", p1.x, p1.y);
82     println!("p2: {}, {}", p2.x, p2.y);
83 }
84 ```
85
86 Alternatively, if we don't control the struct's definition, or mutable shared
87 ownership is truly required, we can use `Rc` and `RefCell`:
88
89 ```
90 use std::cell::RefCell;
91 use std::rc::Rc;
92
93 struct MyStruct { s: u32 }
94
95 fn main() {
96     let mut x = Rc::new(RefCell::new(MyStruct{ s: 5u32 }));
97     let y = x.clone();
98     x.borrow_mut().s = 6;
99     println!("{}", x.borrow().s);
100 }
101 ```
102
103 With this approach, x and y share ownership of the data via the `Rc` (reference
104 count type). `RefCell` essentially performs runtime borrow checking: ensuring
105 that at most one writer or multiple readers can access the data at any one time.
106
107 If you wish to learn more about ownership in Rust, start with the chapter in the
108 Book:
109
110 https://doc.rust-lang.org/book/ch04-00-understanding-ownership.html