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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use rand::Rng;
use std::{io, cmp::Ordering};
use colored::Colorize;
fn gen_random() -> u32 {
return rand::thread_rng().gen_range(0..10);
// rand::thread_rng().gen_range(0..10) // can return variables by removing return keyword and the ";"
}
fn take_input() -> u32 {
let mut input = String::new();
println!("Enter an integer: ");
io::stdin().read_line(&mut input).expect("Failed to read line");
// Handling the user not entering a number within the scope
let input = match input.trim().parse(){
Ok(num) => {
if num < 1 || num > 10{
println!("Please enter a number between 1 and 10.");
take_input()
}
else{
num
}
},
Err(_) => {
println!("Invalid input. Please type a number.");
take_input()
}
};
input
}
pub fn random_num() {
let _num: u32 = gen_random();
//* Typical approach
// for i in 0..3{
// let stdin = take_input();
// if stdin > _num{
// println!("Your guess {} is lower than the number.", stdin);
// }
// else if stdin < _num{
// println!("Your guess {} is higher than the number.", stdin);
// }
// }
//* Typical approach
// if num == stdin{
// println!("\nCorrect Guess. :)\n\nThe number is: {}", stdin);
// }
// else{
// println!("\nIncorrect guess. :(\n\nYou guessed: {}\nThe number was: {}\n", stdin, num)
// }
//* Match statement:
for _i in 0..3{
let stdin = take_input();
match stdin.cmp(&_num){
Ordering::Less => println!("{} {} {}", "Your guess".red(), stdin, "is lower than the number.".red()),
Ordering::Greater => println!("{} {} {}", "Your guess".red(), stdin, "is higher than the number.".red()),
Ordering::Equal => {
println!("{} {}", "Your guess".green(), "is correct.\n\nU WIN!!".green());
break
}
}
}
println!("The number was: {}", _num);
//* Match statement(typical):
// let is_equal = stdin == _num;
// match is_equal {
// true => println!("\nCorrect Guess. :)\n\nThe number is: {}", stdin.to_string().green()),
// false => println!("\nIncorrect guess. :(\n\nYou guessed: {}\nThe number was: {}\n", stdin, _num.to_string().red()),
// _ => println!("Some error that can never be encountered.")
// }
// The match has to be exhaustive, i.e. the value provided must match one value, to handle no match, "_" is used
}