mirror of
https://github.com/ArthurDanjou/hellorust.git
synced 2026-01-14 12:14:35 +01:00
First project of Rust
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
/target
|
||||
.idea
|
||||
9
Cargo.toml
Normal file
9
Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "hellorust"
|
||||
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.4"
|
||||
37
src/main.rs
Normal file
37
src/main.rs
Normal file
@@ -0,0 +1,37 @@
|
||||
use std::cmp::Ordering;
|
||||
use std::io;
|
||||
use rand::Rng;
|
||||
|
||||
fn main() {
|
||||
println!("Welcome in the Guess Game");
|
||||
println!("Try to guess my number");
|
||||
|
||||
let secret_number = rand::thread_rng().gen_range(1..101);
|
||||
|
||||
loop {
|
||||
println!("Please enter 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,
|
||||
};
|
||||
|
||||
|
||||
match guess.cmp(&secret_number) {
|
||||
Ordering::Less => println!("Too small"),
|
||||
Ordering::Greater => println!("Too big"),
|
||||
Ordering::Equal => {
|
||||
println!("You win! The result was {}", secret_number);
|
||||
break;
|
||||
},
|
||||
}
|
||||
|
||||
println!("You guessed: {}", guess);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user