Skip to content

Instantly share code, notes, and snippets.

@Najaf
Created June 4, 2016 23:25
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 Najaf/67953d09f477b78995adaa919b6e284d to your computer and use it in GitHub Desktop.
Save Najaf/67953d09f477b78995adaa919b6e284d to your computer and use it in GitHub Desktop.
use std::io;
use std::io::Write;
use std::str::FromStr;
fn prompt<F>(question: &str) -> Result<F, F::Err>
where F: FromStr
{
let mut answer = String::new();
print!("{} ", question);
let _ = io::stdout().flush();
let _ = io::stdin().read_line(&mut answer);
answer.trim().to_string().parse::<F>()
}
fn main() {
let width: u32 = prompt("Width in feet").unwrap();
let length: u32 = prompt("Length in feet").unwrap();
let area = width * length;
let square_feet_per_gallon = 350;
let gallons;
if 0 == area % square_feet_per_gallon {
gallons = area / square_feet_per_gallon;
} else {
gallons = (area / square_feet_per_gallon) + 1;
}
println!("You will need to purchase {} gallons to cover {} square feet",
gallons,
area);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment