diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..935ea89 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,45 @@ +{ + // 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 'learn'", + "cargo": { + "args": [ + "build", + "--bin=learn", + "--package=learn" + ], + "filter": { + "name": "learn", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'learn'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=learn", + "--package=learn" + ], + "filter": { + "name": "learn", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..7798b73 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "learn-rust" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..50a1e6e --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "learn-rust" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/README.md b/README.md new file mode 100644 index 0000000..66d6e1b --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# Learn Rust Lang + +I'll be stashing all the code artifacts I write while learning rust lang here. + +## How to run + +```bash +$ cargo run +``` \ No newline at end of file diff --git a/src/guessing_game.rs b/src/guessing_game.rs new file mode 100644 index 0000000..5b8c4d2 --- /dev/null +++ b/src/guessing_game.rs @@ -0,0 +1,16 @@ +// use is similar to import in other languages +use std::io; +pub fn run() { + // mut means mutable, meaning we can change the value later + let mut guess = String::new(); + + println!("Welcome to ~Guess the number!~"); + println!("Please input your guess."); + + io::stdin() + // & means reference, meaning we're using the same value + .read_line(&mut guess) + .expect("Failed to read line"); + + println!("You guessed: {}", guess); +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..f907fbc --- /dev/null +++ b/src/main.rs @@ -0,0 +1,35 @@ +// use is similar to import in other languages +use std::io; +// mod to import modules from other files +mod guessing_game; + +// clear screen function +fn clear_screen() { + print!("{}[2J", 27 as char); +} + +fn main() { + clear_screen(); + + // list down all the modules and let the user to select which one to run + println!("Which module do you want to run?"); + println!("1. Guessing Game"); + println!("2. Exit"); + + let mut input: String = String::new(); + + io::stdin() + .read_line(&mut input) + .expect("Failed to read line"); + + let input: u32 = input.trim().parse().expect("Please type a number!"); + + match input { + 1 => { + clear_screen(); + guessing_game::run(); + } + 2 => println!("Exiting..."), + _ => println!("Invalid input!"), + } +}