first bit of guessing game ch2
This commit is contained in:
parent
590c683d98
commit
915ba1a499
8
guessing_game/Cargo.toml
Normal file
8
guessing_game/Cargo.toml
Normal 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
26
guessing_game/src/main.rs
Normal 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 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}");
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user