Created
January 18, 2022 19:41
-
-
Save lbiaggi/3f2b3db4376deed286e7c3f08ab59e30 to your computer and use it in GitHub Desktop.
Calculator with support for overflow/underflow
This file contains hidden or 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
use std::env; | |
fn sub(a: u64, b:u64) -> u64 { | |
a.wrapping_sub(b) | |
} | |
fn add(a:u64, b:u64) -> u64 { | |
a.wrapping_add(b) | |
} | |
fn main() { | |
let args: Vec<String> = env::args().collect(); | |
let result: u64 = if &args[3] == "add" { | |
add( | |
u64::from_str_radix(&args[1].trim_start_matches("0x"),16).unwrap(), | |
u64::from_str_radix(&args[2].trim_start_matches("0x"),16).unwrap(), | |
)} | |
else { | |
sub( | |
u64::from_str_radix(&args[1].trim_start_matches("0x"),16).unwrap(), | |
u64::from_str_radix(&args[2].trim_start_matches("0x"),16).unwrap(), | |
)}; | |
println!("0x{:x}", result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment