Skip to content

Confusing closure example #1611

Open
Open
@sharptrip

Description

@sharptrip

Hi,

it's just caught my eye, that closures from this example don't really explain what a capture means.

let closure_annotated = |i: i32| -> i32 { i + 1 };
let closure_inferred = |i | i + 1 ;

As a totally new to rust I've got an impression that closures in rust capture the whole outer scope disregards of where the closure is defined in code, e.g., before or after the captured variable.

Upd. and then I realised that, probably, this example should never be read as "an example of capturing". What confused me is the beginning explaining what a closure is, giving its probably the most important characteristic first.

Still I would add a capture example here, otherwise it seems just like another way of defining a function (which is probably true as well), with even more confusing variable i having the same name as a function/closure argument i.

Best regards,
Mikhail

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions