This commit is contained in:
viridian 2024-05-10 13:16:50 +02:00
parent f2c9ebb818
commit 4b33b8340f
Signed by: viridian
GPG key ID: DCD4DF95CE23FE8C

View file

@ -1,26 +1,22 @@
use rand::Rng; use rand::Rng;
fn main() { fn main() {
let mut number:i128 = 0; let mut number: i128 = 0;
loop { loop {
let rng:i128 = rand::thread_rng().gen_range(1..=1000000000000000000000000); let rng: i128 = rand::thread_rng().gen_range(1..=1000000000000000000000000);
println!("Do you want to add {} to the number", rng); println!("Do you want to add {} to the number", rng);
println!("Your number is {}(y/n)", number); println!("Your number is {}(y/n)", number);
let mut input: String = String::new(); let mut input: String = String::new();
std::io::stdin().read_line(&mut input).expect("Failed to read line"); std::io::stdin()
let input: String = input.trim().parse().unwrap(); .read_line(&mut input)
let yes:String = String::from("y"); .expect("Failed to read line");
let no = String::from("no"); let input = input.trim(); // Parse is for numbers we use letters here. A &str is more usefull in this case as we dont want to mutate it
if input == yes { match input { // Match is a better way then chaining if/else expressions.
let number = number+=rng; "y" => number += rng, // let expression would have shadowed the original variable and would have droped to soon. Use add assign insted
"n" => continue,
_ => {
println!("Option unknown")
}
} }
else if input == no {
}
else {
println!("Invalid input");
}
} }
} }