File tree Expand file tree Collapse file tree 3 files changed +8
-38
lines changed Expand file tree Collapse file tree 3 files changed +8
-38
lines changed Original file line number Diff line number Diff line change 30
30
- [ Solution] ( types-and-values/solution.md )
31
31
- [ Control Flow Basics] ( control-flow-basics.md )
32
32
- [ Blocks and Scopes] ( control-flow-basics/blocks-and-scopes.md )
33
- - [ Scopes and Shadowing] ( control-flow-basics/blocks-and-scopes/scopes.md )
34
33
- [ ` if ` Expressions] ( control-flow-basics/if.md )
35
34
- [ ` match ` Expressions] ( control-flow-basics/match.md )
36
35
- [ Loops] ( control-flow-basics/loops.md )
Original file line number Diff line number Diff line change @@ -4,8 +4,6 @@ minutes: 5
4
4
5
5
# Blocks and Scopes
6
6
7
- ## Blocks
8
-
9
7
A block in Rust contains a sequence of expressions, enclosed by braces ` {} ` .
10
8
Each block has a value and a type, which are those of the last expression of the
11
9
block:
@@ -19,14 +17,22 @@ fn main() {
19
17
z - y
20
18
};
21
19
println!("x: {x}");
20
+ // println!("y: {y}");
22
21
}
23
22
```
24
23
25
24
If the last expression ends with ` ; ` , then the resulting value and type is ` () ` .
26
25
26
+ A variable's scope is limited to the enclosing block.
27
+
27
28
<details >
28
29
29
30
- You can show how the value of the block changes by changing the last line in
30
31
the block. For instance, adding/removing a semicolon or using a ` return ` .
31
32
33
+ - Demonstrate that attempting to access ` y ` outside of its scope won't compile.
34
+
35
+ - Values are effectively "deallocated" when they go out of their scope, even if
36
+ their data on the stack is still there.
37
+
32
38
</details >
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments