Last active
May 18, 2018 08:30
-
-
Save literadix/0f34fd9ca5ecd875d8e8bccc49f33568 to your computer and use it in GitHub Desktop.
Parse hex value from command line arguments in rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For Masud | |
use std::env; | |
fn main() { | |
let args: Vec<String> = env::args().collect(); | |
match args.len() { | |
2 => { | |
match u32::from_str_radix(&args[1], 16) { | |
Ok(n) => { | |
println!("{}", unsafe { std::mem::transmute::<u32, i32>(n) }); | |
} | |
Err(err) => println!("Error: {:?}", err), | |
} | |
} | |
_ => { | |
println!("usage example: hexvalue ABCD"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Improve error handling even more:
https://blog.burntsushi.net/rust-error-handling/