Blog: Rustic Bits

You can forbid shadowing using clippy. Just #[deny(shadow_same, shadow_reuse, shadow_unrelated)].

I think shadowing with reusing the original value is mostly OK – for example let x = x + 1 in a recursive function to increment the recursion count, or let mut x = x to make something mutable within a scope only. Or let out = out.into_inner() to strip the buffer off a BufWriter.

Shadowing with something completely unrelated can lead to bugs however. In the end it boils down to a tradeoff, as so many things in programming.

1 Like