From 68fa27e20a8089b69254d977964d8871bbb3492b Mon Sep 17 00:00:00 2001 From: Georg Hopp Date: Thu, 21 Nov 2019 19:03:58 +0100 Subject: [PATCH] First plays with cargo and rust --- .gitignore | 1 + guessing_game/.gitignore | 2 + guessing_game/Cargo.lock | 91 +++++++++++++++++++++++++++++++++++++++ guessing_game/Cargo.toml | 8 ++++ guessing_game/src/main.rs | 36 ++++++++++++++++ hello/.gitignore | 2 + hello/Cargo.lock | 6 +++ hello/Cargo.toml | 7 +++ hello/src/main.rs | 3 ++ 9 files changed, 156 insertions(+) create mode 100644 .gitignore create mode 100644 guessing_game/.gitignore create mode 100644 guessing_game/Cargo.lock create mode 100644 guessing_game/Cargo.toml create mode 100644 guessing_game/src/main.rs create mode 100644 hello/.gitignore create mode 100644 hello/Cargo.lock create mode 100644 hello/Cargo.toml create mode 100644 hello/src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ec1830 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +**/.cargo/* diff --git a/guessing_game/.gitignore b/guessing_game/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/guessing_game/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/guessing_game/Cargo.lock b/guessing_game/Cargo.lock new file mode 100644 index 0000000..959e92c --- /dev/null +++ b/guessing_game/Cargo.lock @@ -0,0 +1,91 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "guessing_game" +version = "0.1.0" +dependencies = [ + "rand 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "libc" +version = "0.2.65" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rand" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)", + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", + "rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[metadata] +"checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" +"checksum libc 0.2.65 (registry+https://github.com/rust-lang/crates.io-index)" = "1a31a0627fdf1f6a39ec0dd577e101440b7db22672c0901fe00a9a6fbb5c24e8" +"checksum rand 0.3.23 (registry+https://github.com/rust-lang/crates.io-index)" = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c" +"checksum rand 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +"checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +"checksum rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" +"checksum rdrand 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +"checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/guessing_game/Cargo.toml b/guessing_game/Cargo.toml new file mode 100644 index 0000000..d02b86b --- /dev/null +++ b/guessing_game/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "guessing_game" +version = "0.1.0" +authors = ["Georg Hopp "] +edition = "2018" + +[dependencies] +rand = "0.3.14" diff --git a/guessing_game/src/main.rs b/guessing_game/src/main.rs new file mode 100644 index 0000000..6135d2e --- /dev/null +++ b/guessing_game/src/main.rs @@ -0,0 +1,36 @@ +use std::io; +use std::cmp::Ordering; +use rand::Rng; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng() + .gen_range(1, 101); + + loop { + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + + let guess: u32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; + + println!("You guessed: {}", guess); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => { + println!("You win!"); + break; + } + } + } +} diff --git a/hello/.gitignore b/hello/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/hello/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/hello/Cargo.lock b/hello/Cargo.lock new file mode 100644 index 0000000..f2d069f --- /dev/null +++ b/hello/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "hello" +version = "0.1.0" + diff --git a/hello/Cargo.toml b/hello/Cargo.toml new file mode 100644 index 0000000..49846d5 --- /dev/null +++ b/hello/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "hello" +version = "0.1.0" +authors = ["Georg Hopp "] +edition = "2018" + +[dependencies] diff --git a/hello/src/main.rs b/hello/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/hello/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}