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

Change attributes of a mutable struct

  • mut

  • Using the mut keyword we can defined a struct to be mutable.

struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let mut a = Point { x: 2, y: 3 };
    println!("{}", a.x);
    println!("{}", a.y);

    a.y = 7;
    println!("{}", a.y);
}
2
3
7