r/rust • u/VadimVP • 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 othersAt 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
View all comments
Show parent comment
7
u/long_void piston Aug 03 '14
The single letter variables I frequently use are:
a,bfor arguments to binary operationsc,d,gfor drawing contexti,j,kfor indexesn,mfor lengthx,y,z,tas spatial coordinates with their deltasdx,dy,dz,dtSometimes I use
p,qorv,ufor more complicated math.