Back to questions
JuniorTheoryCommon
What does the program print?
fn main() {
let y = {
let x = 3;
x + 1
};
println!("{y}");
}4
- Expecting a compile error over the missing
;afterx + 1: a block's last expression is written without;on purpose, and it becomes the block's value. - Expecting
3fromlet x = 3;: aletis a statement and gives no value; the block's value is its last expression,x + 1.
- What would
yhold if the block's last line werex + 1;? - Why is
xno longer visible after the block's closing}?