Gültigkeitsbereiche und Verschattungen
A variable's scope is limited to the enclosing block.
Sie können Variablen verschatten (shadow), sowohl solche aus äußeren Gültigkeitsbereichen (outer scopes) als auch Variablen aus dem gleichen Gültigkeitsbereich:
Speaker Notes
- Show that a variable's scope is limited by adding a
b
in the inner block in the last example, and then trying to access it outside that block. - Shadowing is different from mutation, because after shadowing both variables' memory locations exist at the same time. Both are available under the same name, depending where you use it in the code.
- A shadowing variable can have a different type.
- Shadowing sieht zunächst obskur aus, ist aber praktisch, um Werte nach
.unwrap()
festzuhalten.