Scope

1

Scope, Bindings, and Visibility

6 minute

C programmers usually think of scope first as name visibility:

void handle(void)
{
    int offset = 0;

    {
        int len = 16;
        offset += len;
    }

    /* len is not visible, offset is still visible */
}

Rust block scope has the same basic rule:

fn handle() {
    let mut offset = 0;

    {
        let len = 16;
        offset += len;
    }

    // len is not visible, offset is still visible
}

But Rust scope is not only about whether a name can be used. It also affects when resources are released, whether a binding can be shadowed, whether a reference can continue to exist, and which names become part of a module’s public API.

Read More