mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-05-16 23:55:42 +02:00
Remove slide on shadowing (#2596)
This commit is contained in:
parent
72c7618cb4
commit
f1459c54e1
@ -30,7 +30,6 @@
|
|||||||
- [Solution](types-and-values/solution.md)
|
- [Solution](types-and-values/solution.md)
|
||||||
- [Control Flow Basics](control-flow-basics.md)
|
- [Control Flow Basics](control-flow-basics.md)
|
||||||
- [Blocks and Scopes](control-flow-basics/blocks-and-scopes.md)
|
- [Blocks and Scopes](control-flow-basics/blocks-and-scopes.md)
|
||||||
- [Scopes and Shadowing](control-flow-basics/blocks-and-scopes/scopes.md)
|
|
||||||
- [`if` Expressions](control-flow-basics/if.md)
|
- [`if` Expressions](control-flow-basics/if.md)
|
||||||
- [`match` Expressions](control-flow-basics/match.md)
|
- [`match` Expressions](control-flow-basics/match.md)
|
||||||
- [Loops](control-flow-basics/loops.md)
|
- [Loops](control-flow-basics/loops.md)
|
||||||
|
@ -4,8 +4,6 @@ minutes: 5
|
|||||||
|
|
||||||
# Blocks and Scopes
|
# Blocks and Scopes
|
||||||
|
|
||||||
## Blocks
|
|
||||||
|
|
||||||
A block in Rust contains a sequence of expressions, enclosed by braces `{}`.
|
A block in Rust contains a sequence of expressions, enclosed by braces `{}`.
|
||||||
Each block has a value and a type, which are those of the last expression of the
|
Each block has a value and a type, which are those of the last expression of the
|
||||||
block:
|
block:
|
||||||
@ -19,14 +17,22 @@ fn main() {
|
|||||||
z - y
|
z - y
|
||||||
};
|
};
|
||||||
println!("x: {x}");
|
println!("x: {x}");
|
||||||
|
// println!("y: {y}");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
If the last expression ends with `;`, then the resulting value and type is `()`.
|
If the last expression ends with `;`, then the resulting value and type is `()`.
|
||||||
|
|
||||||
|
A variable's scope is limited to the enclosing block.
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
|
|
||||||
- You can show how the value of the block changes by changing the last line in
|
- You can show how the value of the block changes by changing the last line in
|
||||||
the block. For instance, adding/removing a semicolon or using a `return`.
|
the block. For instance, adding/removing a semicolon or using a `return`.
|
||||||
|
|
||||||
|
- Demonstrate that attempting to access `y` outside of its scope won't compile.
|
||||||
|
|
||||||
|
- Values are effectively "deallocated" when they go out of their scope, even if
|
||||||
|
their data on the stack is still there.
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
@ -1,35 +0,0 @@
|
|||||||
# Scopes and Shadowing
|
|
||||||
|
|
||||||
A variable's scope is limited to the enclosing block.
|
|
||||||
|
|
||||||
You can shadow variables, both those from outer scopes and variables from the
|
|
||||||
same scope:
|
|
||||||
|
|
||||||
```rust,editable
|
|
||||||
fn main() {
|
|
||||||
let a = 10;
|
|
||||||
println!("before: {a}");
|
|
||||||
{
|
|
||||||
let a = "hello";
|
|
||||||
println!("inner scope: {a}");
|
|
||||||
|
|
||||||
let a = true;
|
|
||||||
println!("shadowed in inner scope: {a}");
|
|
||||||
}
|
|
||||||
|
|
||||||
println!("after: {a}");
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
<details>
|
|
||||||
|
|
||||||
- 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 looks obscure at first, but is convenient for holding on to values
|
|
||||||
after `.unwrap()`.
|
|
||||||
|
|
||||||
</details>
|
|
Loading…
x
Reference in New Issue
Block a user