Safe conversion from u32
to i32
:
use std::convert::TryFrom;
fn main() {
let good: u32 = 50;
let ok: i32 = i32::try_from(good).unwrap();
println!("{} -> {}", good, ok);
let bad: u32 = u32::MAX;
let err: i32 = i32::try_from(bad).unwrap(); // PANIC
println!("{} -> {}", bad, err);
}