1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-01-09 09:11:50 +02:00

Update scopes-shadowing.md (#216)

Adding the definition of what shadowing is and how it is different from mutation.
This commit is contained in:
Igor Petruk 2023-01-23 11:38:11 +00:00 committed by GitHub
parent 71ce061cc2
commit d1588e4f2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -22,6 +22,8 @@ fn main() {
<details>
* Definition: Shadowing is different from mutation, because after shadowing both variable's 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 looks obscure at first, but is convenient for holding on to values after `.unwrap()`.
* The following code demonstrates why the compiler can't simply reuse memory locations when shadowing an immutable variable in a scope, even if the type does not change.