Guessing Game with numbers
This commit is contained in:
parent
3d664633eb
commit
87b7fbdee4
20 changed files with 107 additions and 4 deletions
19
Cargo.lock
generated
19
Cargo.lock
generated
|
@ -1,4 +1,23 @@
|
|||
[root]
|
||||
name = "hello_world"
|
||||
version = "0.0.1"
|
||||
dependencies = [
|
||||
"rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "rand"
|
||||
version = "0.3.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[metadata]
|
||||
"checksum libc 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)" = "88ee81885f9f04bff991e306fea7c1c60a5f0f9e409e99f6b40e3311a3363135"
|
||||
"checksum rand 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "022e0636ec2519ddae48154b028864bdce4eaf7d35226ab8e65c611be97b189d"
|
||||
|
|
10
Cargo.toml
10
Cargo.toml
|
@ -4,6 +4,12 @@ name = "hello_world"
|
|||
version = "0.0.1"
|
||||
authors = [ "Clara Benedicta Maria Mueller <clara.mueller@vilera.de>" ]
|
||||
|
||||
|
||||
[[bin]]
|
||||
name = "test"
|
||||
path = "/media/win_e/Clara_Daten/etc/Programmierung/Rust/src/test.rs"
|
||||
name = "main"
|
||||
path = "/media/win_e/Clara_Daten/etc/Programmierung/Rust/src/main.rs"
|
||||
|
||||
|
||||
[dependencies]
|
||||
|
||||
rand="0.3.0"
|
||||
|
|
15
Cargo.toml_backup
Executable file
15
Cargo.toml_backup
Executable file
|
@ -0,0 +1,15 @@
|
|||
[package]
|
||||
|
||||
name = "hello_world"
|
||||
version = "0.0.1"
|
||||
authors = [ "Clara Benedicta Maria Mueller <clara.mueller@vilera.de>" ]
|
||||
|
||||
|
||||
[[bin]]
|
||||
name = "test"
|
||||
path = "/media/win_e/Clara_Daten/etc/Programmierung/Rust/src/test.rs"
|
||||
|
||||
|
||||
[dependencies]
|
||||
|
||||
rand="0.3.0"
|
56
main.rs
Executable file
56
main.rs
Executable file
|
@ -0,0 +1,56 @@
|
|||
// another rust programm
|
||||
// let's get started ^.^
|
||||
|
||||
extern crate rand; // actually using rand (declared in dependencies of 'Cargo.toml')
|
||||
|
||||
// rust only uses few things by default ('prelude') -> add your needs!
|
||||
use std::io; // use io library from the standard library
|
||||
use std::cmp::Ordering; // use ordering type
|
||||
use rand::Rng; // using rands function rand::thread_rng()
|
||||
|
||||
|
||||
// main function
|
||||
fn main() {
|
||||
println!("guess a number!"); // macro, prints string
|
||||
|
||||
let secret_number = rand::thread_rng() // get copy of random number generator
|
||||
.gen_range(1, 101); // methods takes 2 arguments -> generate random number between (inclusive on the lower bound, but exclusive on the upper bound -> 1 - 100)
|
||||
|
||||
// println!("The secret number is: {}", secret_number); // print out secret number // just for testing -> runs the game!
|
||||
|
||||
|
||||
loop { // building infinity loop
|
||||
|
||||
println!("please input your guess!."); // macro, prints string
|
||||
|
||||
let mut guess = String::new(); // let -> create 'variable bindings'; String -> string type (UTF-8 encoded bit of text); '::' -> associate function; 'new()' -> created empty
|
||||
let foo = 5; // 'foo' is immutable
|
||||
let mut bar = 5; //'bar' is mutable
|
||||
|
||||
io::stdin().read_line(&mut guess) // calling associated function // without std::io -> written 'std::io::stdin()' //returns handle to standard input
|
||||
// read_line() -> calling method (like associate function, only on parcticular instant of type) an handle
|
||||
// read_line():&mut guess -> passing argument to mutable reference of guess
|
||||
.expect("failed to read line"); // io::Result encodes error handling information // not successful -> panic! with message "failed to read line"
|
||||
// alternative written: 'io::stdin().read_line(&mut guess).expect("failed to read line");'
|
||||
|
||||
// let guess: u32 = guess.trim().parse() // 'shadow' guess with new one -> convert string into u32 // 'trim()' eliminates whitespace // 'prase()' -> parses string into number
|
||||
// .expect("Please type a number!"); // stop programm if not a number
|
||||
|
||||
let guess: u32 = match guess.trim().parse() { // 'handling error' -> 'match' instead 'expect()' // result returned by prase()
|
||||
Ok(num) => num, // success -> set name num on unwrapped Ok value (integer)
|
||||
Err(_) => continue, // failure -> don't care of kind of error -> catch all _(everything not ok) // 'continue' -> move to next iteration of loop (aka ignore all errors)
|
||||
};
|
||||
|
||||
println!("you guessed: {}", guess); // print out saved string from STDIN
|
||||
println!("FOO: {} & BAR: {}", foo, bar); // print out string variables
|
||||
|
||||
match guess.cmp(&secret_number) { // cmp can called on anything comparable, takes references to thing to compare // returns odering type // match -> determine exactly what typ of ordering (ordering = enum (enumeration)) // guess + secret_number have to be same type for comparision!
|
||||
Ordering::Less => println!("To small!"), // ordering enum -> 3 possible variants (less/greater/equal)
|
||||
Ordering::Greater => println!("To big!"),
|
||||
Ordering::Equal => {
|
||||
println!("You win!");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1 @@
|
|||
17a9309d5cd6c961
|
|
@ -0,0 +1 @@
|
|||
{"rustc":9749998217168482212,"target":17950575461743581120,"profile":14528549471338536373,"local":{"variant":"MtimeBased","fields":[[1492077381,811763100],[47,109,101,100,105,97,47,119,105,110,95,101,47,67,108,97,114,97,95,68,97,116,101,110,47,101,116,99,47,80,114,111,103,114,97,109,109,105,101,114,117,110,103,47,82,117,115,116,47,115,114,99,47,116,97,114,103,101,116,47,100,101,98,117,103,47,46,102,105,110,103,101,114,112,114,105,110,116,47,104,101,108,108,111,95,119,111,114,108,100,45,48,99,56,102,52,100,97,55,102,57,51,99,57,99,53,99,47,100,101,112,45,98,105,110,45,109,97,105,110,45,48,99,56,102,52,100,97,55,102,57,51,99,57,99,53,99]]},"features":"None","deps":[["rand v0.3.15",127043405904794477]],"rustflags":[]}
|
Binary file not shown.
|
@ -1 +1 @@
|
|||
6d8524286a203399
|
||||
430c19affbf6a977
|
|
@ -1 +1 @@
|
|||
{"rustc":9749998217168482212,"target":10994546209011070589,"profile":14528549471338536373,"local":{"variant":"MtimeBased","fields":[[1492017954,643537700],[47,109,101,100,105,97,47,119,105,110,95,101,47,67,108,97,114,97,95,68,97,116,101,110,47,101,116,99,47,80,114,111,103,114,97,109,109,105,101,114,117,110,103,47,82,117,115,116,47,115,114,99,47,116,97,114,103,101,116,47,100,101,98,117,103,47,46,102,105,110,103,101,114,112,114,105,110,116,47,104,101,108,108,111,95,119,111,114,108,100,45,57,52,51,56,49,55,100,52,54,99,48,52,52,53,53,56,47,100,101,112,45,98,105,110,45,116,101,115,116,45,57,52,51,56,49,55,100,52,54,99,48,52,52,53,53,56]]},"features":"None","deps":[],"rustflags":[]}
|
||||
{"rustc":9749998217168482212,"target":10994546209011070589,"profile":14528549471338536373,"local":{"variant":"MtimeBased","fields":[[1492072499,359251700],[47,109,101,100,105,97,47,119,105,110,95,101,47,67,108,97,114,97,95,68,97,116,101,110,47,101,116,99,47,80,114,111,103,114,97,109,109,105,101,114,117,110,103,47,82,117,115,116,47,115,114,99,47,116,97,114,103,101,116,47,100,101,98,117,103,47,46,102,105,110,103,101,114,112,114,105,110,116,47,104,101,108,108,111,95,119,111,114,108,100,45,57,52,51,56,49,55,100,52,54,99,48,52,52,53,53,56,47,100,101,112,45,98,105,110,45,116,101,115,116,45,57,52,51,56,49,55,100,52,54,99,48,52,52,53,53,56]]},"features":"None","deps":[["rand v0.3.15",127043405904794477]],"rustflags":[]}
|
BIN
target/debug/.fingerprint/libc-5dc7b85e748840b4/dep-lib-libc-5dc7b85e748840b4
Executable file
BIN
target/debug/.fingerprint/libc-5dc7b85e748840b4/dep-lib-libc-5dc7b85e748840b4
Executable file
Binary file not shown.
|
@ -0,0 +1 @@
|
|||
20054fec61dda75a
|
|
@ -0,0 +1 @@
|
|||
{"rustc":9749998217168482212,"target":12335968779674043000,"profile":14528549471338536373,"local":{"variant":"Precalculated","fields":["0.2.21"]},"features":"Some([\"default\", \"use_std\"])","deps":[],"rustflags":[]}
|
BIN
target/debug/.fingerprint/rand-c9d9fbdab2355ee4/dep-lib-rand-c9d9fbdab2355ee4
Executable file
BIN
target/debug/.fingerprint/rand-c9d9fbdab2355ee4/dep-lib-rand-c9d9fbdab2355ee4
Executable file
Binary file not shown.
|
@ -0,0 +1 @@
|
|||
6dd302f84d59c301
|
|
@ -0,0 +1 @@
|
|||
{"rustc":9749998217168482212,"target":16630976334449306048,"profile":14528549471338536373,"local":{"variant":"Precalculated","fields":["0.3.15"]},"features":"None","deps":[["libc v0.2.21",6532433197170361632]],"rustflags":[]}
|
BIN
target/debug/deps/liblibc-5dc7b85e748840b4.rlib
Executable file
BIN
target/debug/deps/liblibc-5dc7b85e748840b4.rlib
Executable file
Binary file not shown.
BIN
target/debug/deps/librand-c9d9fbdab2355ee4.rlib
Executable file
BIN
target/debug/deps/librand-c9d9fbdab2355ee4.rlib
Executable file
Binary file not shown.
BIN
target/debug/deps/main-0c8f4da7f93c9c5c
Executable file
BIN
target/debug/deps/main-0c8f4da7f93c9c5c
Executable file
Binary file not shown.
BIN
target/debug/main
Executable file
BIN
target/debug/main
Executable file
Binary file not shown.
1
target/debug/main.d
Executable file
1
target/debug/main.d
Executable file
|
@ -0,0 +1 @@
|
|||
/media/win_e/Clara_Daten/etc/Programmierung/Rust/src/target/debug/main: /media/win_e/Clara_Daten/etc/Programmierung/Rust/src/main.rs
|
Loading…
Add table
Add a link
Reference in a new issue