Variable Assignment Styles with Let and Mut and Scope


#1

Is there a general use rule for selecting let over mut in real world projects? Let and mut are are still constrained by scope?

struct Point3d {
    x: i32,
    y: i32,
    z: i32,
}

fn main () {

let mut point = Point3d { x: 0, y: 0, z: 0 };
point = Point3d { y: 1, .. point };
point = Point3d { y: 2, .. point };
let point = Point3d { y: 9, .. point };
// point = Point3d { y: 10, .. point };   E0384 error

 println!("`x`: {}", point.x);
 println!("`y`: {}", point.y);
 println!("`z`: {}", point.z); 


}

#2

I think the general rule is to use let and to avoid mut.It is also worth noting that shadowing is allowed by the Rust compiler and the style guide. So it often makes sense to rebind a name to new value, rather then mutate existing bindings:

let x = compute()
let x = compute_more(x)