Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Learning Rust with a Guessing Game 🦀 #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions Rust/guessing_game/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Rust/guessing_game/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
21 changes: 21 additions & 0 deletions Rust/guessing_game/README.md
Original file line number Diff line number Diff line change
@@ -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!
75 changes: 75 additions & 0 deletions Rust/guessing_game/src/main.rs
Original file line number Diff line number Diff line change
@@ -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.

*/