]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_error_codes/src/error_codes/E0508.md
New upstream version 1.55.0+dfsg1
[rustc.git] / compiler / rustc_error_codes / src / error_codes / E0508.md
CommitLineData
60c5eb7d
XL
1A value was moved out of a non-copy fixed-size array.
2
3Erroneous code example:
4
5```compile_fail,E0508
6struct NonCopy;
7
8fn main() {
9 let array = [NonCopy; 1];
10 let _value = array[0]; // error: cannot move out of type `[NonCopy; 1]`,
11 // a non-copy fixed-size array
12}
13```
14
15The first element was moved out of the array, but this is not
16possible because `NonCopy` does not implement the `Copy` trait.
17
18Consider borrowing the element instead of moving it:
19
20```
21struct NonCopy;
22
23fn main() {
24 let array = [NonCopy; 1];
25 let _value = &array[0]; // Borrowing is allowed, unlike moving.
26}
27```
28
29Alternatively, if your type implements `Clone` and you need to own the value,
30consider borrowing and then cloning:
31
32```
33#[derive(Clone)]
34struct NonCopy;
35
36fn main() {
37 let array = [NonCopy; 1];
38 // Now you can clone the array element.
39 let _value = array[0].clone();
40}
41```
136023e0
XL
42
43If you really want to move the value out, you can use a destructuring array
44pattern to move it:
45
46```
47struct NonCopy;
48
49fn main() {
50 let array = [NonCopy; 1];
51 // Destructuring the array
52 let [_value] = array;
53}
54```