diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2022-02-06 04:13:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-02-06 04:13:29 +0100 |
| commit | 58bfe72f52d2f5bb32ca4d46ec84de5dfad1bda9 (patch) | |
| tree | ad956d6f15e1dd0d245aa5a0eee45d885d5d4925 /compiler/rustc_error_codes/src | |
| parent | 88fb06a1f331926bccb448acdb52966fd1ec8a92 (diff) | |
| parent | badb81a6129b84a9f01223ab73a56ffe52bf33ce (diff) | |
| download | rust-58bfe72f52d2f5bb32ca4d46ec84de5dfad1bda9.tar.gz rust-58bfe72f52d2f5bb32ca4d46ec84de5dfad1bda9.zip | |
Rollup merge of #91939 - GKFX:feature-91866, r=cjgillot
Clarify error on casting larger integers to char Closes #91836 with changes to E0604.md and a `span_help`.
Diffstat (limited to 'compiler/rustc_error_codes/src')
| -rw-r--r-- | compiler/rustc_error_codes/src/error_codes/E0604.md | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/compiler/rustc_error_codes/src/error_codes/E0604.md b/compiler/rustc_error_codes/src/error_codes/E0604.md index adbf76509ed..806f0001c60 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0604.md +++ b/compiler/rustc_error_codes/src/error_codes/E0604.md @@ -6,11 +6,16 @@ Erroneous code example: 0u32 as char; // error: only `u8` can be cast as `char`, not `u32` ``` -As the error message indicates, only `u8` can be cast into `char`. Example: +`char` is a Unicode Scalar Value, an integer value from 0 to 0xD7FF and +0xE000 to 0x10FFFF. (The gap is for surrogate pairs.) Only `u8` always fits in +those ranges so only `u8` may be cast to `char`. + +To allow larger values, use `char::from_u32`, which checks the value is valid. ``` -let c = 86u8 as char; // ok! -assert_eq!(c, 'V'); +assert_eq!(86u8 as char, 'V'); // ok! +assert_eq!(char::from_u32(0x3B1), Some('α')); // ok! +assert_eq!(char::from_u32(0xD800), None); // not a USV. ``` For more information about casts, take a look at the Type cast section in |
