From dc67163bac89adf3f6f3ba31c595009f64cef36c Mon Sep 17 00:00:00 2001 From: daleclack Date: Fri, 1 Jul 2022 12:41:08 +0800 Subject: [PATCH] Add guessing_game --- Rust_tests/guessing_game/.vscode/launch.json | 47 ++++++++++++ Rust_tests/guessing_game/Cargo.lock | 75 ++++++++++++++++++++ Rust_tests/guessing_game/Cargo.toml | 9 +++ Rust_tests/guessing_game/src/main.rs | 34 +++++++++ 4 files changed, 165 insertions(+) create mode 100644 Rust_tests/guessing_game/.vscode/launch.json create mode 100644 Rust_tests/guessing_game/Cargo.lock create mode 100644 Rust_tests/guessing_game/Cargo.toml create mode 100644 Rust_tests/guessing_game/src/main.rs diff --git a/Rust_tests/guessing_game/.vscode/launch.json b/Rust_tests/guessing_game/.vscode/launch.json new file mode 100644 index 0000000..cb91c0d --- /dev/null +++ b/Rust_tests/guessing_game/.vscode/launch.json @@ -0,0 +1,47 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'guessing_game'", + "cargo": { + "args": [ + "build", + "--bin=guessing_game", + "--package=guessing_game", + "--release" + ], + "filter": { + "name": "guessing_game", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'guessing_game'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=guessing_game", + "--package=guessing_game", + "--release" + ], + "filter": { + "name": "guessing_game", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/Rust_tests/guessing_game/Cargo.lock b/Rust_tests/guessing_game/Cargo.lock new file mode 100644 index 0000000..cb8c7e6 --- /dev/null +++ b/Rust_tests/guessing_game/Cargo.lock @@ -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.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "guessing_game" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "libc" +version = "0.2.126" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836" + +[[package]] +name = "ppv-lite86" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872" + +[[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.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/Rust_tests/guessing_game/Cargo.toml b/Rust_tests/guessing_game/Cargo.toml new file mode 100644 index 0000000..7eda67a --- /dev/null +++ b/Rust_tests/guessing_game/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "guessing_game" +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.5" diff --git a/Rust_tests/guessing_game/src/main.rs b/Rust_tests/guessing_game/src/main.rs new file mode 100644 index 0000000..53402f0 --- /dev/null +++ b/Rust_tests/guessing_game/src/main.rs @@ -0,0 +1,34 @@ +use rand::Rng; +use std::cmp::Ordering; +use std::io; + +fn main() { + println!("Guess number!"); + + let test_number = rand::thread_rng().gen_range(1..100); //Generate a random number + println!("The test number: {}", test_number); + loop { + let mut guess = String::new(); + + println!("Input a number"); + + io::stdin().read_line(&mut guess).expect("Can't read line"); //Read the number + + let guess: u32 = match guess.trim().parse(){ //Use match to handle errors when parse string to number + Ok(num) => num, + Err(_) => continue, + }; //Transform string input to number + + println!("The number you guess is: {}", guess); //Print the numbers + + match guess.cmp(&test_number) { + //Compare the input number and the generated number + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal =>{ + println!("Winned!"); + break; + } , + } + } +}