]> Zhao Yanbai Git Server - acecode.git/commitdiff
learn rust: guessing_game
authoracevest <zhaoyanbai@126.com>
Mon, 30 Aug 2021 07:58:53 +0000 (15:58 +0800)
committeracevest <zhaoyanbai@126.com>
Mon, 30 Aug 2021 07:58:53 +0000 (15:58 +0800)
.gitignore
learn/rust/guessing_game/Cargo.lock [new file with mode: 0644]
learn/rust/guessing_game/Cargo.toml [new file with mode: 0644]
learn/rust/guessing_game/src/main.rs [new file with mode: 0644]
learn/rust/hello_cargo/src/main.rs

index 31f18ebb85a4fc68412703ce05c5de89382f7935..961c400b46a63cd34198c281bf8791eb4518ec24 100644 (file)
@@ -14,3 +14,4 @@ dvwa
 build
 Debug
 OneNetMiniBoard
+**/rust/**/target
diff --git a/learn/rust/guessing_game/Cargo.lock b/learn/rust/guessing_game/Cargo.lock
new file mode 100644 (file)
index 0000000..ee8651c
--- /dev/null
@@ -0,0 +1,85 @@
+# 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.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi",
+]
+
+[[package]]
+name = "guessing_game"
+version = "0.1.0"
+dependencies = [
+ "rand",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.101"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3cb00336871be5ed2c8ed44b60ae9959dc5b9f08539422ed43f09e34ecaeba21"
+
+[[package]]
+name = "ppv-lite86"
+version = "0.2.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
+
+[[package]]
+name = "rand"
+version = "0.8.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8"
+dependencies = [
+ "libc",
+ "rand_chacha",
+ "rand_core",
+ "rand_hc",
+]
+
+[[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.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7"
+dependencies = [
+ "getrandom",
+]
+
+[[package]]
+name = "rand_hc"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7"
+dependencies = [
+ "rand_core",
+]
+
+[[package]]
+name = "wasi"
+version = "0.10.2+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
diff --git a/learn/rust/guessing_game/Cargo.toml b/learn/rust/guessing_game/Cargo.toml
new file mode 100644 (file)
index 0000000..172ae61
--- /dev/null
@@ -0,0 +1,9 @@
+[package]
+name = "guessing_game"
+version = "0.1.0"
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+rand = "0.8.3"
diff --git a/learn/rust/guessing_game/src/main.rs b/learn/rust/guessing_game/src/main.rs
new file mode 100644 (file)
index 0000000..b071fde
--- /dev/null
@@ -0,0 +1,39 @@
+use std::io;
+use std::cmp::Ordering;
+use rand::Rng;
+
+fn main() {
+    println!("GUESS THE NUMBER");
+
+
+    let secret_number = rand::thread_rng().gen_range(0..100);
+
+    loop {
+
+        println!("please input your 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(s) => {
+                println!("error: {}", s);
+                continue;
+            },
+        };
+
+        println!("you guess: {}", guess);
+
+        match guess.cmp(&secret_number) {
+            Ordering::Less => println!("too small"),
+            Ordering::Equal => {
+                println!("right");
+                break;
+            },
+            Ordering::Greater => println!("too big!"),
+        }
+    }
+
+    println!("secret number: {}", secret_number);
+}
index e7a11a969c037e00a796aafeff6258501ec15e9a..55150c560bab92db878dd3d4d787bd0d4451d89f 100644 (file)
@@ -1,3 +1,4 @@
 fn main() {
     println!("Hello, world!");
+    print!("Hello, world!\n");
 }