Skip to content

Instantly share code, notes, and snippets.

@archer884
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save archer884/47e7a1b36b335c95bcf4 to your computer and use it in GitHub Desktop.
Save archer884/47e7a1b36b335c95bcf4 to your computer and use it in GitHub Desktop.
Tip calculator
struct TipResult {
amt: f64,
tip: f64,
}
fn main() {
let args: Vec<_> = std::env::args().collect();
let tip = match &args[..] {
[_, ref amt] => get_tip(amt.parse().ok(), Some(0.15f64)),
[_, ref amt, ref pct] => get_tip(amt.parse().ok(), pct.parse().ok()),
_ => None,
};
match tip {
Some(tip) => println!("${:.2} on ${:.2}", tip.tip, tip.amt),
None => println!("USAGE: {} AMOUNT <decimal> [TIP PERCENT] <integer>", args[0]),
}
}
fn get_tip(amt: Option<f64>, pct: Option<f64>) -> Option<TipResult> {
if amt.is_none() || pct.is_none() {
return None;
}
let amt = amt.unwrap();
let pct = pct.unwrap() / 100f64;
Some(TipResult {
amt: amt.clone(),
tip: &amt * &pct,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment