Why can't I cast a `u32` to `char`?

2019-02-16 18:54发布

I'm trying to learn Rust and I got caught up thinking about how chars are 4 bytes wide. I can cast a char to a u32 and it works out (they are both 4 bytes wide), however, when I cast from a u32 to a char, Rust complains:

fn main() {
    let pizza_hex: u32 = 0x1f355;
    let pizza: char = '                

标签: rust
1条回答
女痞
2楼-- · 2019-02-16 19:39

Every char is a valid u32 value, but not every u32 value is a valid char.

The property of chars 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 above char::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);
查看更多
登录 后发表回答