]>
Commit | Line | Data |
---|---|---|
60c5eb7d XL |
1 | An invalid cast was attempted. |
2 | ||
3 | Erroneous code examples: | |
4 | ||
5 | ```compile_fail,E0605 | |
6 | let x = 0u8; | |
7 | x as Vec<u8>; // error: non-primitive cast: `u8` as `std::vec::Vec<u8>` | |
8 | ||
9 | // Another example | |
10 | ||
11 | let v = core::ptr::null::<u8>(); // So here, `v` is a `*const u8`. | |
12 | v as &u8; // error: non-primitive cast: `*const u8` as `&u8` | |
13 | ``` | |
14 | ||
15 | Only primitive types can be cast into each other. Examples: | |
16 | ||
17 | ``` | |
18 | let x = 0u8; | |
19 | x as u32; // ok! | |
20 | ||
21 | let v = core::ptr::null::<u8>(); | |
22 | v as *const i8; // ok! | |
23 | ``` | |
24 | ||
25 | For more information about casts, take a look at the Type cast section in | |
26 | [The Reference Book][1]. | |
27 | ||
28 | [1]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions |