Skip to content

Commit 81cd752

Browse files
committed
Add explanation for E0508
1 parent 763f923 commit 81cd752

File tree

1 file changed

+44
-1
lines changed

1 file changed

+44
-1
lines changed

src/librustc_borrowck/diagnostics.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,50 @@ You can find more information about borrowing in the rust-book:
911911
http://doc.rust-lang.org/stable/book/references-and-borrowing.html
912912
"##,
913913

914+
E0508: r##"
915+
A value was moved out of a non-copy fixed-size array.
916+
917+
Example of erroneous code:
918+
919+
```compile_fail
920+
struct NonCopy;
921+
922+
fn main() {
923+
let array = [NonCopy; 1];
924+
let _value = array[0]; // error: cannot move out of type `[NonCopy; 1]`,
925+
// a non-copy fixed-size array
926+
}
927+
```
928+
929+
The first element was moved out of the array, but this is not
930+
possible because `NonCopy` does not implement the `Copy` trait.
931+
932+
Consider borrowing the element instead of moving it:
933+
934+
```
935+
struct NonCopy;
936+
937+
fn main() {
938+
let array = [NonCopy; 1];
939+
let _value = &array[0]; // Borrowing is allowed, unlike moving.
940+
}
941+
```
942+
943+
Alternatively, if your type implements `Clone` and you need to own the value,
944+
consider borrowing and then cloning:
945+
946+
```
947+
#[derive(Clone)]
948+
struct NonCopy;
949+
950+
fn main() {
951+
let array = [NonCopy; 1];
952+
// Now you can clone the array element.
953+
let _value = array[0].clone();
954+
}
955+
```
956+
"##,
957+
914958
E0509: r##"
915959
This error occurs when an attempt is made to move out of a value whose type
916960
implements the `Drop` trait.
@@ -1012,6 +1056,5 @@ register_diagnostics! {
10121056
E0385, // {} in an aliasable location
10131057
E0388, // {} in a static location
10141058
E0503, // cannot use `..` because it was mutably borrowed
1015-
E0508, // cannot move out of type `..`, a non-copy fixed-size array
10161059
E0524, // two closures require unique access to `..` at the same time
10171060
}

0 commit comments

Comments
 (0)