Add example of guessing game with custom type for validation
This commit is contained in:
parent
f637759454
commit
4c4b703928
2 changed files with 68 additions and 0 deletions
10
guessing_game_ng/Cargo.toml
Normal file
10
guessing_game_ng/Cargo.toml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "guessing_game_ng"
|
||||
version = "0.1.0"
|
||||
authors = ["laurens <miers132@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
rand = "0.5.5"
|
||||
58
guessing_game_ng/src/main.rs
Normal file
58
guessing_game_ng/src/main.rs
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
use std::io;
|
||||
use std::cmp::Ordering;
|
||||
use rand::Rng;
|
||||
|
||||
const MIN_NUMBER: i32 = 1;
|
||||
const MAX_NUMBER: i32 = 100;
|
||||
|
||||
pub struct Guess {
|
||||
value: i32,
|
||||
}
|
||||
|
||||
impl Guess {
|
||||
pub fn new(value: i32) -> Guess {
|
||||
if value < MIN_NUMBER || value > MAX_NUMBER {
|
||||
panic!("Guess value must be between 1 and 100, got {}.", value);
|
||||
}
|
||||
|
||||
Guess { value }
|
||||
}
|
||||
|
||||
pub fn value(&self) -> i32 {
|
||||
self.value
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let secret_number = rand::thread_rng().gen_range(MIN_NUMBER, MAX_NUMBER + 1);
|
||||
|
||||
println!("Guess the number between [{}, {}] {}", MIN_NUMBER, MAX_NUMBER, secret_number);
|
||||
|
||||
loop {
|
||||
let mut guess = String::new();
|
||||
println!("Input guess:");
|
||||
io::stdin().read_line(&mut guess).expect("Failed to read line");
|
||||
|
||||
let guess: i32 = match guess.trim().parse() {
|
||||
Ok(num) => num,
|
||||
Err(_) => {
|
||||
println!("That ain't a number!");
|
||||
continue;
|
||||
}
|
||||
};
|
||||
|
||||
let guess: Guess = Guess::new(guess);
|
||||
|
||||
println!("You guessed: {}", guess.value);
|
||||
|
||||
match guess.value.cmp(&secret_number) {
|
||||
Ordering::Less => println!("Too small!"),
|
||||
Ordering::Greater => println!("Too big!"),
|
||||
Ordering::Equal => {
|
||||
println!("Congrats, you did it!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue