Skip to content

Commit 2eafd3a

Browse files
author
0xHaris
committed
guessing game completed
0 parents  commit 2eafd3a

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

Cargo.lock

+91
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "guessing_game"
3+
version = "0.1.0"
4+
authors = ["0xHaris <0xbitshot@>@gmail.com"]
5+
edition = "2021"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
11+
rand = "0.3.14"

src/main.rs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
use rand::Rng;
2+
use std::cmp::Ordering;
3+
use std::io;
4+
fn main() {
5+
println!("Guess the number!");
6+
7+
let secret_number = rand::thread_rng().gen_range(1, 101);
8+
9+
loop {
10+
println!("Please input your guess!");
11+
12+
let mut guess = String::new(); //immutable data types
13+
14+
io::stdin()
15+
.read_line(&mut guess)
16+
.expect("Failed to read line");
17+
18+
let guess: u32 = match guess.trim().parse() {
19+
Ok(n) => n,
20+
Err(_) => continue,
21+
};
22+
23+
println!("You guessed: {}", guess);
24+
25+
match guess.cmp(&secret_number) {
26+
Ordering::Less => println!("Too small!"),
27+
Ordering::Greater => println!("Too big"),
28+
Ordering::Equal => {
29+
println!("You win!");
30+
break;
31+
}
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)