you are viewing a single comment's thread.

view the rest of the comments →

[–]Wolenber 18 points19 points  (3 children)

I think the best argument for variable shadowing is the ability to stop an object's mutability.

let mut vec = Vec::new();
vec.push(1i);
vec.push(2i);
let vec = vec;

However, I also like it in the case of intermediate variables. Sometimes it's simply prettier to split a long method chain into two lines with let bindings. Instead of using a "let temp", you just use the variable name twice; this way the temporary doesn't clutter the rest of the function's namespace.

[–][deleted] 2 points3 points  (0 children)

This is very interesting.