File tree Expand file tree Collapse file tree 1 file changed +8
-2
lines changed Expand file tree Collapse file tree 1 file changed +8
-2
lines changed Original file line number Diff line number Diff line change @@ -4,8 +4,6 @@ minutes: 5
44
55# Blocks and Scopes
66
7- ## Blocks
8-
97A block in Rust contains a sequence of expressions, enclosed by braces ` {} ` .
108Each block has a value and a type, which are those of the last expression of the
119block:
@@ -19,14 +17,22 @@ fn main() {
1917 z - y
2018 };
2119 println!("x: {x}");
20+ // println!("y: {y}");
2221}
2322```
2423
2524If the last expression ends with ` ; ` , then the resulting value and type is ` () ` .
2625
26+ A variable's scope is limited to the enclosing block.
27+
2728<details >
2829
2930- You can show how the value of the block changes by changing the last line in
3031 the block. For instance, adding/removing a semicolon or using a ` return ` .
3132
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+
3238</details >
You can’t perform that action at this time.
0 commit comments