This commit is contained in:
Zengtudor 2024-07-09 13:03:20 +08:00
parent 8f20f9b39b
commit dbfb31c16e
4 changed files with 109 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

75
Cargo.lock generated Normal file
View File

@ -0,0 +1,75 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "guess_number"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "libc"
version = "0.2.155"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c"
[[package]]
name = "ppv-lite86"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"

7
Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "guess_number"
version = "0.1.0"
edition = "2021"
[dependencies]
rand = "0.8.5"

26
src/main.rs Normal file
View File

@ -0,0 +1,26 @@
use std::io;
use std::cmp::Ordering;
use rand::Rng;
fn main() {
let number_range: std::ops::RangeInclusive<usize> = 1..=100;
println!("Please enter a number to guess a number between {} and {} !",number_range.start(),number_range.end());
let rand_number: usize = rand::thread_rng().gen_range(number_range).try_into().expect("random a number failed :(\n");
let mut enter_string: String = String::new();
let mut enter_number:usize;
for i in 1..=6 {
println!("The {} time input",i);
enter_string.clear();
io::stdin().read_line(&mut enter_string).expect("read the message you entered failed ! :(\n");
enter_number = enter_string.trim().parse().expect("Parse the number you entered failed! :(\n");
match enter_number.cmp(&rand_number) {
Ordering::Less => println!("The number you entered was too small!"),
Ordering::Equal => {
println!("You win !\nThe number is {}",rand_number);
return;
},
Ordering::Greater => println!("The number you entered was too big!")
}
}
println!("You failed . \n The number is {}",rand_number);
}