diff --git a/guessing_game/.gitignore b/guessing_game/.gitignore new file mode 100644 index 0000000..0eec03d --- /dev/null +++ b/guessing_game/.gitignore @@ -0,0 +1,11 @@ +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk \ No newline at end of file diff --git a/guessing_game/Cargo.toml b/guessing_game/Cargo.toml new file mode 100644 index 0000000..a80ef9e --- /dev/null +++ b/guessing_game/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "guessing_game" +version = "0.1.0" +authors = ["Emily J. "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.7.3" diff --git a/guessing_game/src/main.rs b/guessing_game/src/main.rs new file mode 100644 index 0000000..17bc99c --- /dev/null +++ b/guessing_game/src/main.rs @@ -0,0 +1,52 @@ +/* + * Simple number guessing game I made using the tutorial at + * https://doc.rust-lang.org/stable/book/ch02-00-guessing-game-tutorial.html + */ + +use std::io; +use std::cmp::Ordering; +use rand::Rng; + +fn main() { + // Generates random number between 0 and 100, thread_rng is the generator we're using + let number = rand::thread_rng().gen_range(0, 101); + let mut tries = 1; + + println!("Choose a number between 0 and 100: "); + + loop { + // Defines a mutable variable that returns a string, don't know why constants exist yet + let mut guess = String::new(); + + // the & symbol in front of mut means its a reference, whatever that means lol + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line."); + + // Remove whitespace and convert the input into an unsigned 32 bit integer, using match statement to handle err + let guess: u32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => { + println!("Invalid input, please type a number!"); + continue; + } + }; + + // Checks whether the guess is higher, lower or equal to the random number + // I added a tries thing myself, that tells you how many turns it took for you to guess the number + match guess.cmp(&number) { + Ordering::Less => { + println!("Higher!"); + tries += 1; + } + Ordering::Greater => { + println!("Lower!"); + tries += 1; + } + Ordering::Equal => { + println!("You win! You took {} tries to guess the number.", tries); + break; + } + } + } +} diff --git a/hello b/hello new file mode 100755 index 0000000..5becca1 Binary files /dev/null and b/hello differ diff --git a/hello.rs b/hello.rs new file mode 100644 index 0000000..e3cb0df --- /dev/null +++ b/hello.rs @@ -0,0 +1,6 @@ +// i want to hug ferris + +fn main() { + println!("Hello World!"); + println!("I'm a Rustacean!"); +} \ No newline at end of file diff --git a/hello_cargo/.gitignore b/hello_cargo/.gitignore new file mode 100644 index 0000000..0039f28 --- /dev/null +++ b/hello_cargo/.gitignore @@ -0,0 +1,10 @@ +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk \ No newline at end of file diff --git a/hello_cargo/Cargo.toml b/hello_cargo/Cargo.toml new file mode 100644 index 0000000..9098105 --- /dev/null +++ b/hello_cargo/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "hello_cargo" +version = "0.1.0" +authors = ["Emily J. "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/hello_cargo/src/main.rs b/hello_cargo/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/hello_cargo/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}