Skip to content

Commit 5439806

Browse files
committed
Add explanation for E0508.
1 parent c39e37a commit 5439806

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

src/librustc_borrowck/diagnostics.rs

+44-1
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,50 @@ You can find more information about borrowing in the rust-book:
967967
http://doc.rust-lang.org/stable/book/references-and-borrowing.html
968968
"##,
969969

970+
E0508: r##"
971+
A value was moved out of a non-copy fixed-size array.
972+
973+
Example of erroneous code:
974+
975+
```compile_fail
976+
struct NonCopy;
977+
978+
fn main() {
979+
let array = [NonCopy; 1];
980+
let _value = array[0]; // error: cannot move out of type `[NonCopy; 1]`,
981+
// a non-copy fixed-size array
982+
}
983+
```
984+
985+
The first element was moved out of the array, but this is not
986+
possible because `NonCopy` does not implement the `Copy` trait.
987+
988+
Consider borrowing the element instead of moving it:
989+
990+
```
991+
struct NonCopy;
992+
993+
fn main() {
994+
let array = [NonCopy; 1];
995+
let _value = &array[0]; // Borrowing is allowed, unlike moving.
996+
}
997+
```
998+
999+
Alternatively, if your type implements `Clone` and you need to own the value,
1000+
consider borrowing and then cloning:
1001+
1002+
```
1003+
#[derive(Clone)]
1004+
struct NonCopy;
1005+
1006+
fn main() {
1007+
let array = [NonCopy; 1];
1008+
// Now you can clone the array element.
1009+
let _value = array[0].clone();
1010+
}
1011+
```
1012+
"##,
1013+
9701014
E0509: r##"
9711015
This error occurs when an attempt is made to move out of a value whose type
9721016
implements the `Drop` trait.
@@ -1067,6 +1111,5 @@ fn main() {
10671111
register_diagnostics! {
10681112
E0385, // {} in an aliasable location
10691113
E0388, // {} in a static location
1070-
E0508, // cannot move out of type `..`, a non-copy fixed-size array
10711114
E0524, // two closures require unique access to `..` at the same time
10721115
}

0 commit comments

Comments
 (0)