restored files
This commit is contained in:
parent
1bfaf3b07a
commit
cc3f76fc67
8 changed files with 101 additions and 0 deletions
11
guessing_game/.gitignore
vendored
Normal file
11
guessing_game/.gitignore
vendored
Normal file
|
@ -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
|
10
guessing_game/Cargo.toml
Normal file
10
guessing_game/Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
[package]
|
||||||
|
name = "guessing_game"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Emily J. <me@emily.im>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
rand = "0.7.3"
|
52
guessing_game/src/main.rs
Normal file
52
guessing_game/src/main.rs
Normal file
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
BIN
hello
Executable file
BIN
hello
Executable file
Binary file not shown.
6
hello.rs
Normal file
6
hello.rs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
// i want to hug ferris
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Hello World!");
|
||||||
|
println!("I'm a Rustacean!");
|
||||||
|
}
|
10
hello_cargo/.gitignore
vendored
Normal file
10
hello_cargo/.gitignore
vendored
Normal file
|
@ -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
|
9
hello_cargo/Cargo.toml
Normal file
9
hello_cargo/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
[package]
|
||||||
|
name = "hello_cargo"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Emily J. <me@emily.im>"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
3
hello_cargo/src/main.rs
Normal file
3
hello_cargo/src/main.rs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
Loading…
Reference in a new issue