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

前端 未结 1 969

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 (

1条回答
  •  情话喂你
    2021-01-01 21:16

    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);
    

    0 讨论(0)
提交回复
热议问题