diff --git a/Rust/guessing_game/Cargo.lock b/Rust/guessing_game/Cargo.lock new file mode 100644 index 0000000..c219dde --- /dev/null +++ b/Rust/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.135" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68783febc7782c6c5cb401fbda4de5a9898be1762314da0bb2c10ced61f18b0c" + +[[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.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" diff --git a/Rust/guessing_game/Cargo.toml b/Rust/guessing_game/Cargo.toml new file mode 100644 index 0000000..d39f664 --- /dev/null +++ b/Rust/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.3" \ No newline at end of file diff --git a/Rust/guessing_game/README.md b/Rust/guessing_game/README.md new file mode 100644 index 0000000..5906de9 --- /dev/null +++ b/Rust/guessing_game/README.md @@ -0,0 +1,21 @@ +Author: NizTheDev +Date: 16/Oct/22 + +This is a beginner project for rust programmers. + +Prerequistites: + +1. Basic programming knowledge in any other programming language +2. cargo // it is a package manager for rust just like npm is for nodejs. +3. rust // installation guide here: https://www.rust-lang.org/tools/install + +How to run this project: + +`cargo run` in the root folder of this project + +Creating a similar project: +`cargo init` in the folder where you want to create the project + +For any tech related queries feel free to reach me out through nizthedev.web.app + +Happy Coding! diff --git a/Rust/guessing_game/src/main.rs b/Rust/guessing_game/src/main.rs new file mode 100644 index 0000000..6319bea --- /dev/null +++ b/Rust/guessing_game/src/main.rs @@ -0,0 +1,75 @@ +// IMPORTS +use rand::Rng; +use std::{cmp::Ordering, io::stdin}; + +// ENTRY POINT FOR PROJECT +fn main() { + // GENERATE a number in range of 1 to 100 ( 1 and 100 inclusive ) + let mut secret_number: i32 = rand::thread_rng().gen_range(1..=100); // check appendix 1. below + + print!("\x1B[2J\x1B[1;1H"); // CLEAR terminal + + println!("I dare you to Guess the unlucky number!"); // PROMPT the user + + // from here on out, all the steps below will be executed indefinitely until broken out of, which is commented on line 52 + + loop { + // DEFINE and INITIALIZE variable called guess with a String + + let mut guess = String::new(); + + // we are passing in guess with this special syntax to give the read_line function ability to change the value of + // this variable in the inner scope of the function. By default it's not allowed in rust. + //check appendix 2. + stdin() + .read_line(&mut guess) + .expect("Failed to read the line!"); + + // the following syntax makes sure that guess is not null, it there is any issues with guess, it will throw error + // check appendix 3. + let guess: i32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => { + print!("\x1B[2J\x1B[1;1H"); + println!("Enter a number you dummy!"); + continue; + } + }; + + // match is basically like switch case + + match guess.cmp(&secret_number) { + Ordering::Less => { + print!("\x1B[2J\x1B[1;1H"); + println!("Too small :/") + } + Ordering::Greater => { + print!("\x1B[2J\x1B[1;1H"); + println!("Too big :(") + } + Ordering::Equal => { + // println!("You won the lottery!~ :)"); + // break; + print!("\x1B[2J\x1B[1;1H"); + println!("The number ran away you dummy!"); + println!("You think you can get a hold of the unlucky number ?"); + println!("Think again!"); + secret_number = rand::thread_rng().gen_range(1..=100); + } + } + println!("Guess again kiddo!"); + } +} + +/* + +APPENDIX + +rust is type safe and memory safe language. + +1. mut is short for mutable which is used to identify if the variable value can be changed or not. +2. rust borrow checker guide: https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html +3. Rust doesn't have the null feature that many other languages have. Null is a value that means there is no value there. + In languages with null, variables can always be in one of two states: null or not- null. + +*/