區塊 (block) 和範疇 (scope)
區塊
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:
fn main() { let z = 13; let x = { let y = 10; println!("y: {y}"); z - y }; println!("x: {x}"); }
If the last expression ends with ;
, then the resulting value and type is ()
.
This slide and its sub-slides should take about 5 minutes.
- 你可以藉由改變區塊中的最後一行來觀察區塊數值的變化。舉例來說,新增或刪除一個分號,或者使用
return
。