Skip to content

Lifetimes in closure return values are inferred to be 'static, unlike with functions #17908

Closed
@ftxqxd

Description

@ftxqxd
let x: || -> &str = || "hello"; // fine
fn x() -> &str { "hello" } // error

This is a result of lifetime elision not existing for closures. (Should it?) This was the only backwards-incompatible part of lifetime elision, and so for backwards-compatibility lifetime annotations should be required for closure types as well, in case lifetime elision is ever added to closures. Also, it’d be more consistent with normal functions.

This applies to return type annotations in closure expressions, too: let x = || -> &str "hello" also compiles without an explicit lifetime annotation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions