Skip to content

Copy doesn't work for a closure argument #29996

@yongqli

Description

@yongqli

I would expect this code [1] to work:

(0..5).flat_map(|i| (0..5).map(|j| i + j)).collect::<Vec<_>>()

since i is Copy. But rustc complains with

error: `i` does not live long enough

I could use move, but in the real code it would take ownership of too much. Shouldn't variables be copied if necessary and what's a succinct workaround?

[1] https://play.rust-lang.org/?gist=3ef7699396e1b712ccd4&version=stable

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