Skip to content

Instantly share code, notes, and snippets.

@rpivo
Last active July 8, 2021 21:00
Show Gist options
  • Save rpivo/f7a77c82b46068f15567a9146dd25333 to your computer and use it in GitHub Desktop.
Save rpivo/f7a77c82b46068f15567a9146dd25333 to your computer and use it in GitHub Desktop.
Using Closures in Rust

Using Closures in Rust

In Rust, inner functions don't automatically have access to outer-scope variables. However, we can create closures using the || {} syntax, which will have access to outer-scope variables.

fn main() {
    let s = "hello";

    let c = || {
        println!("{}", s);
    };
    
    c(); // hello
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment