I\'m trying to learn Rust and I got caught up thinking about how char
s are 4 bytes wide. I can cast a char
to a u32
and it works out (
Every char
is a valid u32
value, but not every u32
value is a valid char
.
The property of char
s holding valid Unicode codepoints factors into memory safety:
Behavior considered undefined
- Invalid values in primitive types, even in private fields and locals:
- A value in a
char
which is a surrogate or abovechar::MAX
.
To convert a u32
to a char
at runtime, try this:
if let Some(pizza_from_hex) = std::char::from_u32(pizza_hex) {
println!("{} == {}", pizza_from_hex, pizza);
}
If you just don't want creepy Unicode glyphs in your character literals, you can use Unicode escape sequences:
let pizza_from_hex = '\u{01f355}';
println!("{} == {}", pizza_from_hex, pizza);