1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2024-12-04 03:25:08 +02:00

Merge pull request #136 from fbornhofen/speaker-notes-variables

Add speaker notes for scopes-shadowing.md
This commit is contained in:
Martin Geisler 2023-01-09 18:44:29 +01:00 committed by GitHub
commit bee9cdab92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,3 +19,19 @@ fn main() {
println!("after: {a}");
}
```
<details>
* 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.
```rust,editable
fn main() {
let a = 1;
let b = &a;
let a = a + 1;
println!("{a} {b}");
}
```
</details>