Rust: guessing game

Source: https://doc.rust-lang.org/book/ch02-00-guessing-game-tutorial.html

Here’s the cheatsheet and summary of the rust tutorial (source)


$ cargo new myproj
$ cd myproj
$ vi src/main.rs  # to edit
$ cargo run 


file: main.rs
use std::io;                            // importing std::io module
fn main() {
    let mut guess = String::new();      // initializing a new mutable variable
    io::stdin()                         
        .read_line(&mut guess)          // mutable reference
        .expect("Failed to read line");
    println!("You guessed: {}", guess);
}

Terms

  • crate: a collection of Rust source code files