first bit of guessing game ch2

This commit is contained in:
LuKe Tidd 2022-12-12 08:42:20 -05:00
parent 590c683d98
commit 915ba1a499
Signed by: luke
GPG Key ID: 75D6600BEF4E8E8F
2 changed files with 34 additions and 0 deletions

8
guessing_game/Cargo.toml Normal file
View File

@ -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]

26
guessing_game/src/main.rs Normal file
View File

@ -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 thats 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}");
}