Blocks and Scopes
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
block:
If the last expression ends with ;
, then the resulting value and type is ()
.
A variable’s scope is limited to the enclosing block.
Speaker Notes
This slide should take about 5 minutes.
-
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
. -
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.