Add colorize
parent
1e18a1ab53
commit
bfb7306522
|
@ -2,12 +2,34 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "colored"
|
||||
version = "2.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd"
|
||||
dependencies = [
|
||||
"atty",
|
||||
"lazy_static",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.6"
|
||||
|
@ -23,9 +45,25 @@ dependencies = [
|
|||
name = "hello_cargo"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"colored",
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.1.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.126"
|
||||
|
@ -73,3 +111,25 @@ name = "wasi"
|
|||
version = "0.10.2+wasi-snapshot-preview1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
|
|
@ -7,3 +7,4 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
rand = "0.8.4"
|
||||
colored = "2"
|
||||
|
|
32
src/main.rs
32
src/main.rs
|
@ -2,17 +2,24 @@ use rand::Rng;
|
|||
use std::cmp::Ordering;
|
||||
use std::io;
|
||||
|
||||
use colored::Colorize;
|
||||
|
||||
fn main() {
|
||||
guess_number();
|
||||
}
|
||||
|
||||
fn guess_number() {
|
||||
loop {
|
||||
println!("== Guess the number! ==\n");
|
||||
println!("{}", "== Guess the number! ==");
|
||||
println!("{}", "Min = 0; Max = 255\n");
|
||||
let mut iterator: u16 = 0;
|
||||
let secret_number: u8 = rand::thread_rng().gen_range(u8::MIN..=u8::MAX);
|
||||
|
||||
let secret_number: u8 = rand::thread_rng().gen_range(u8::MIN..=u8::MAX);
|
||||
println!("The secret number is: {}.", secret_number);
|
||||
println!("Please input your guess: ");
|
||||
loop {
|
||||
if iterator == 0 {
|
||||
println!("{}", "Please input your guess: ".bright_blue());
|
||||
} else {
|
||||
println!("{}", "Please input your new guess: ".bright_blue());
|
||||
}
|
||||
|
||||
let mut guess = String::new();
|
||||
|
||||
|
@ -22,16 +29,23 @@ fn guess_number() {
|
|||
|
||||
let guess: u8 = match guess.trim().parse() {
|
||||
Ok(num) => num,
|
||||
Err(_) => continue
|
||||
Err(_) => continue,
|
||||
};
|
||||
|
||||
match guess.cmp(&secret_number) {
|
||||
Ordering::Less => println!("Too small!"),
|
||||
Ordering::Greater => println!("Too big!"),
|
||||
Ordering::Less => println!("{}", "Too small.".bright_green()),
|
||||
Ordering::Greater => println!("{}", "Too big.".bright_red()),
|
||||
Ordering::Equal => {
|
||||
println!("You win!");
|
||||
if iterator == 0 {
|
||||
println!("{}", "\nWOW! You won on the first try!".bright_purple());
|
||||
break;
|
||||
}
|
||||
|
||||
println!("\nYou win :D ({} attempts)", iterator);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
iterator += 1;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue