23

In Rust it is encouraged to shadow variables:

But wait, doesn’t the program already have a variable named guess? It does, but Rust allows us to shadow the previous value of guess with a new one.

Won't this feature just introduce problems like:

  • hard to follow code (easier to create bugs)
  • accessing variables when one intended to access a different variable (creates bugs)

I have based this information from my own experience and the following sources: 1 2 3 4 5

What are the underlying reasons behind the decision to include variable shadowing?

It does have it advantages as to just create guess and not guess_str vs guess_int. There are both advantages and disadvantages.

What convinced the inventors of Rust that the advantages are greater than the disadvantages?

The programming world seems divided about this; some languages only issue warnings and discourage shadowing, some languages disallow it explicitly, some allow it and others even encourage it. What is the reasoning?

If possible I'd like to understand more, and a complete answer would possibly include:

  • What kind of advantages/disadvantages are there?
  • What are the use cases for shadow variables?
  • When not to use them in Rust?
  • What do different people from different programming background have to keep in mind? (and which pitfalls not to fall into)
Gizmo
  • 1,990
  • 1
  • 24
  • 50
  • 1
    Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/206449/discussion-on-question-by-gizmo-what-is-the-rationale-behind-allowing-variable-s). – deceze Jan 22 '20 at 14:20
  • 2
    The Book shows a clear reasoning behind their decision, but the way you present your question makes it seem opinionated and imprecise. Shadowing allows us to reuse variable names rather than creating unique ones; it allows us to transform variables without making them mut; it lets us convert type without manually creating two vars of different types (makes things concise by automating this); shadowing is optional (you don't have to do it) and not extremely complex or difficult to read. If there's a real problem with shadowing, have you considered asking about one problem at a time? – Wolfpack'08 Jan 25 '20 at 13:18
  • 4
    @Wolfpack'08 It feels like you did not read the question, I know why the example uses shadowing. If you read further than the title only then you know I'm asking for the rationale of including shadowing _at all_ into Rust. – Gizmo Jan 27 '20 at 00:31
  • Shadowing allows you to change the spot in memory referenced by a variable name, but changing the value of a spot in memory is still impossible (unless you mut it). Not sure why this question was marked as opinion-based. More on this SO answer: https://stackoverflow.com/a/53235438/5612019 – Luigi Apr 26 '23 at 18:49

1 Answers1

32

Because it was initially supported and never removed:

It’s more like we never forbade shadowing, since it just fell out of the implementation of the compiler.

As I recall, Graydon floated the idea of forbidding shadowing, but I stuck up for the feature, nobody else really cared, and so it stayed.

- pcwalton

See also:

Community
  • 1
  • 1
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366