]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0604.md
Rollup merge of #93556 - dtolnay:trailingcomma, r=cjgillot
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0604.md
1 A cast to `char` was attempted on a type other than `u8`.
2
3 Erroneous code example:
4
5 ```compile_fail,E0604
6 0u32 as char; // error: only `u8` can be cast as `char`, not `u32`
7 ```
8
9 `char` is a Unicode Scalar Value, an integer value from 0 to 0xD7FF and
10 0xE000 to 0x10FFFF. (The gap is for surrogate pairs.) Only `u8` always fits in
11 those ranges so only `u8` may be cast to `char`.
12
13 To allow larger values, use `char::from_u32`, which checks the value is valid.
14
15 ```
16 assert_eq!(86u8 as char, 'V'); // ok!
17 assert_eq!(char::from_u32(0x3B1), Some('α')); // ok!
18 assert_eq!(char::from_u32(0xD800), None); // not a USV.
19 ```
20
21 For more information about casts, take a look at the Type cast section in
22 [The Reference Book][1].
23
24 [1]: https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions