Safely Casting a “char” to an “i64” and an “i64” to a “char”

⚓ rust    📅 2025-07-15    👤 surdeus    👁️ 1      

surdeus

Where x is a char and “a” and y is an i64 and 61, I am aware that I can write x as i64 and y as char; but I want to safely cast them.

I tried what is in a Rust playground which has:

fn main() {
    let x = 'a';
    let y = 61i64;
    
    let x_as_an_i64: i64 = x.try_into().unwrap();
    let y_as_a_char: char = y.try_into().unwrap();
    
    assert_eq!(x_as_an_i64, y);
    assert_eq!(y_as_a_char, x);
}

But Cargo interestingly fails exiting with status 101 and saying:

   Compiling playground v0.0.1 (/playground)
error[E0425]: cannot find value `y_as_an_i64` in this scope
 --> src/main.rs:9:16
  |
9 |     assert_eq!(y_as_an_i64, x);
  |                ^^^^^^^^^^^ help: a local variable with a similar name exists: `x_as_an_i64`

error[E0277]: the trait bound `i64: TryFrom<char>` is not satisfied
 --> src/main.rs:5:30
  |
5 |     let x_as_an_i64: i64 = x.try_into().unwrap();
  |                              ^^^^^^^^ the trait `From<char>` is not implemented for `i64`
  |
  = help: the following other types implement trait `From<T>`:
            `i64` implements `From<bool>`
            `i64` implements `From<deranged::RangedI64<MIN, MAX>>`
            `i64` implements `From<i16>`
            `i64` implements `From<i32>`
            `i64` implements `From<i8>`
            `i64` implements `From<jiff::util::rangeint::ri128<MIN, MAX>>`
            `i64` implements `From<jiff::util::rangeint::ri16<MIN, MAX>>`
            `i64` implements `From<jiff::util::rangeint::ri32<MIN, MAX>>`
          and 9 others
  = note: required for `char` to implement `Into<i64>`
  = note: required for `i64` to implement `TryFrom<char>`
  = note: required for `char` to implement `TryInto<i64>`

error[E0277]: the trait bound `char: TryFrom<i64>` is not satisfied
 --> src/main.rs:6:31
  |
6 |     let y_as_a_char: char = y.try_into().unwrap();
  |                               ^^^^^^^^ the trait `From<i64>` is not implemented for `char`
  |
  = help: the following other types implement trait `From<T>`:
            `char` implements `From<Char>`
            `char` implements `From<u8>`
  = note: required for `i64` to implement `Into<char>`
  = note: required for `char` to implement `TryFrom<i64>`
  = note: required for `i64` to implement `TryInto<char>`

Some errors have detailed explanations: E0277, E0425.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `playground` (bin "playground") due to 3 previous errors

What is interesting is that a char is smaller than an i64 but it only implements From<Char> and From<u8> and an i64 implements traits from From<bool> to From<i32> but does not implement From<char>.

I am looking for the safest way that never panics. What would you suggest?

3 posts - 3 participants

Read full topic

🏷️ rust_feed