guessing_game
This commit is contained in:
parent
cc529de765
commit
4a437ba5dd
3 changed files with 49 additions and 0 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -8,3 +8,5 @@ Cargo.lock
|
||||||
|
|
||||||
# These are backup files generated by rustfmt
|
# These are backup files generated by rustfmt
|
||||||
**/*.rs.bk
|
**/*.rs.bk
|
||||||
|
|
||||||
|
run
|
||||||
|
|
9
guessing_game/Cargo.toml
Normal file
9
guessing_game/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "guessing_game"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rand = "0.8.5"
|
38
guessing_game/src/main.rs
Normal file
38
guessing_game/src/main.rs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
//Libs
|
||||||
|
use std::io;
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
use rand::Rng;
|
||||||
|
//Funcs
|
||||||
|
fn main()
|
||||||
|
{
|
||||||
|
println!("Hello, world!\n");
|
||||||
|
let secret_number = rand::thread_rng().gen_range(1..=10);
|
||||||
|
println!("\t--> Guessing Game <--");
|
||||||
|
println!("Secret Number = {secret_number}");
|
||||||
|
loop
|
||||||
|
{
|
||||||
|
println!("Give a guess ↓ ");
|
||||||
|
let mut guess = String::new();
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut guess)
|
||||||
|
.expect("Failed to read line");
|
||||||
|
let guess: u32 = match
|
||||||
|
guess.trim()
|
||||||
|
.parse()
|
||||||
|
{
|
||||||
|
Ok(num) => num,
|
||||||
|
Err(_) => continue,
|
||||||
|
};
|
||||||
|
println!("You Guessed = {guess}");
|
||||||
|
match guess.cmp(&secret_number)
|
||||||
|
{
|
||||||
|
Ordering::Less => println!("Too small"),
|
||||||
|
Ordering::Greater => println!("Too big"),
|
||||||
|
Ordering::Equal =>
|
||||||
|
{
|
||||||
|
println!("You win");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue