Learn Rust features with digital flashcards!

Variables and types

Show basic binding and mutability: create x = 5 (immutable) and y = 10 (mutable), try to reassign x (should not compile), increment y by 5, then print both.

Starter code:

fn main() {
    // Your code here
}

Solution: Variables and Types

fn main() {
    let x = 5;
    // x = 6; // This would cause a compile error!

    let mut y = 10;
    y += 5;

    println!("x = {}", x);
    println!("y = {}", y);
}

Explanation

Immutable by default: - Rust variables are immutable by default - let x = 5 creates an immutable binding - Trying to reassign x causes a compile error

Mutable variables: - Use let mut to make a variable mutable - y += 5 is shorthand for y = y + 5

Key takeaways: - Immutability is the default in Rust (safety first!) - Explicit mut keyword makes intent clear - Compiler prevents accidental mutations

Common patterns:

let x = 5;           // immutable
let mut y = 10;      // mutable
const MAX: i32 = 100; // constant (always immutable)
fn main() { // Your code here }
Got It No Clue