statement

Rust Local item declarations

A function declared inside a block is available within that scope. Use a small helper when it is only needed in one part of the program. Unlike a closure, a nested function cannot implicitly capture an outer local binding. Expected output (not executed): 8 Why: The outer value is passed explicitly as an argument. Common mistake: Do not refer to `width` directly in the body of `double`; it is not captured.

Formal Syntax & Structure

rust syntaxrust
{ fn helper(...) { ... } }

Working Example

Example Snippetrust
fn main() {    let width = 4;    fn double(value: i32) -> i32 { value * 2 }    println!("{}", double(width));}
Rust Reference