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:

fn main() {
    let z = 13;
    let x = {
        let y = 10;
        println!("y: {y}");
        z - y
    };
    println!("x: {x}");
}

اگر آخرین عبارت با ; پایان یابد، مقدار و نوع بازگشتی () است.

This slide and its sub-slides should take about 5 minutes.
  • می‌توانید نشان دهید که چگونه با تغییر خط آخر بلاک مقدار و نوع بازگشتی تغییر می‌کند. به عنوان مثال با اضافه کردن یا حذف کردن یک ; یا با استفاده از کلید واژه return تغییرات را اعمال کنید.