« Back to home

Rust Number Conversion - Don't Follow the Book...

Bottom-Line Up Front: I think as should be harder to use for Integer conversion in Rust. I recommend you use TryFrom/TryInto instead. Here’s how I recommend doing it (more complete example of right/wrong at the end): use std::convert::TryInto; fn main() -> Result<(), Box<dyn std::error::Error>> { let orig: u64 = u64::MAX; let ti_u64: u64 = orig.try_into()?; let ti_i64: i64 = orig.try_into()?; let ti_u32: u32 = orig.try_into()?; Ok(()) } I’m at an intermediate level with the Rust programming language.…

Read more »