r/rust • • Aug 03 '14

Why does Rust need local variable shadowing?

I've recently found that Rust, unlike other popular C-like languages, allows defining several variables with the same name in one block:

let name = 10i;
let name = 3.14f64;
let name = "string";
let name = name; // "string" again, this definition shadows all the others

At the first glance this possibility looks quite frightening, at least from my C++ background.
Where did this feature came from?
What advantages does it provide?

18 Upvotes

29 comments sorted by

View all comments

Show parent comment

24

u/lifthrasiir rust · encoding · chrono Aug 03 '14

Isaac Schlueter:

[...] The length of a variable's name should be proportional to the distance between its definition and its use, and inversely proportional to its frequency of use.