-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
47 lines (38 loc) · 1.47 KB
/
Copy pathmain.rs
File metadata and controls
47 lines (38 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use rand::Rng;
use std::cmp::Ordering;
use std::io;
//primary function
fn main() {
println!("Guess the number!"); //will prompt the user for input
let secret_number = rand::thread_rng().gen_range(1..=100); //generates a secret number
let mut attempts = 5; // initialize attempts variable
loop {
println!("Please input your guess. You have {} attempts left", attempts);
let mut guess = String::new(); //allows for multiple inputs
io::stdin()
.read_line(&mut guess) //reads user input
.expect("Failed to read line"); //if something goes wrong this will print
//allows the user to input string values
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
println!("You guessed: {guess}"); //shows user their guess
//logic to decide if the user input was correct
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"), //number is higher
Ordering::Greater => println!("Too big!"), //number is lower
Ordering::Equal => { //the correct number
println!("You win!"); //informs the user of victory
break;
}
}
// Decrement attempts after each guess
attempts -= 1;
// check if attempts are over
if attempts == 0 {
println!("You lose!");
break;
}
}
}