-
-
Save Najaf/60ddc39c41befd442ca633bfc73ae966 to your computer and use it in GitHub Desktop.
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
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 people: u8 = prompt("How many people?").unwrap(); | |
let pizzas: u8 = prompt("How many pizzas?").unwrap(); | |
let slices_per_pizza = 8; | |
let total_slices = pizzas * slices_per_pizza; | |
let slices_per_person = total_slices / people; | |
let leftovers = total_slices % people; | |
println!("{} people with {} pizzas", people, pizzas); | |
println!("Each person gets {} slices of pizza", slices_per_person); | |
println!("There are {} leftover pieces", leftovers); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment