Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Number in a mutable variable

We can make a variable mutable by adding the mut keyword to the declaration. In this example we use numbers as strings have addition constraints.

fn main() {
    let mut num = 2;
    println!("{num}");

    num += 1;
    println!("{num}");
}

The output:

2
3

  • let
  • mut