From 915ba1a499b3f2096611cfe045b3057520cfa4c6 Mon Sep 17 00:00:00 2001 From: Luke Tidd Date: Mon, 12 Dec 2022 08:42:20 -0500 Subject: [PATCH] first bit of guessing game ch2 --- guessing_game/Cargo.toml | 8 ++++++++ guessing_game/src/main.rs | 26 ++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) create mode 100644 guessing_game/Cargo.toml create mode 100644 guessing_game/src/main.rs diff --git a/guessing_game/Cargo.toml b/guessing_game/Cargo.toml new file mode 100644 index 0000000..78c94fe --- /dev/null +++ b/guessing_game/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "guessing_game" +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/guessing_game/src/main.rs b/guessing_game/src/main.rs new file mode 100644 index 0000000..a0334e5 --- /dev/null +++ b/guessing_game/src/main.rs @@ -0,0 +1,26 @@ +// some things are automatically loaded in every rust program +// https://doc.rust-lang.org/std/prelude/index.html +// for extras, import them with `use`: +use std::io; + +fn main() { + println!("Guess the number!"); + println!("input your guess"); + // vars are immutable by default, change this + // The :: syntax in the ::new line indicates that new is an _associated function_ of the String + // a function that’s implemented on a type + let mut guess = String::new(); + // could still call this without importing it via std::io::stdin + // this can be one line, it is broken up for readability. + io::stdin() + // & indicates that this argument is a reference, + // references are also immutable by default so &mut is needed + .read_line(&mut guess) + // expect handles the Err variant by crashing the program. There are other ways to handle + // errors. + .expect("Failed to read line"); + // read_line returns a `result` value. result is an enum. Each possible state is a variant. + // Result's variants are Ok and Err + + println!("you guessed: {guess}"); +}