mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-03-25 15:38:01 +02:00
Added blocks and scopes section (#1822)
This commit is contained in:
parent
b5b06b5aa9
commit
fccdc0d69a
@ -37,6 +37,7 @@
|
|||||||
- [`break` and `continue`](control-flow-basics/break-continue.md)
|
- [`break` and `continue`](control-flow-basics/break-continue.md)
|
||||||
- [Labels](control-flow-basics/break-continue/labels.md)
|
- [Labels](control-flow-basics/break-continue/labels.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)
|
||||||
- [Functions](control-flow-basics/functions.md)
|
- [Functions](control-flow-basics/functions.md)
|
||||||
- [Macros](control-flow-basics/macros.md)
|
- [Macros](control-flow-basics/macros.md)
|
||||||
- [Exercise: Collatz Sequence](control-flow-basics/exercise.md)
|
- [Exercise: Collatz Sequence](control-flow-basics/exercise.md)
|
||||||
|
@ -24,40 +24,9 @@ fn main() {
|
|||||||
|
|
||||||
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 `()`.
|
||||||
|
|
||||||
## 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>
|
<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`.
|
||||||
- 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 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()`.
|
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
35
src/control-flow-basics/blocks-and-scopes/scopes.md
Normal file
35
src/control-flow-basics/blocks-and-scopes/scopes.md
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
# 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 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()`.
|
||||||
|
|
||||||
|
</details>
|
Loading…
x
Reference in New Issue
Block a user