Variables and Mutability

202307032103
Status: #idea
Tags: Rust

Variables and Mutability

Warning

let can only be used inside a function

let apples = 5; // Immutable
let mut bananas = 5; // Mutable

Shadowing

Let's you reuse the variable name

let mut guess = String::new();

io::stdin()
    .read_line(&mut guess)
    .expect("Failed to read line");

let guess: u32 = guess.trim().parse().expect("Please type a number!");
Important

Constants are computed at compile-time
Immutable variables are computed at run-time


References

  1. https://rust-book.cs.brown.edu/ch02-00-guessing-game-tutorial.html