Skip to content

Instantly share code, notes, and snippets.

@x3rAx
Last active May 18, 2020 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save x3rAx/d7c428feb68df7f63b94fb30b1fc0d61 to your computer and use it in GitHub Desktop.
Save x3rAx/d7c428feb68df7f63b94fb30b1fc0d61 to your computer and use it in GitHub Desktop.
Rust Notes

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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment