Skip to content

&str lifetime when variable is borrowed as argument (not captured) by closure #155998

@ghost

Description

fn myfn(str: &str) {
 let c = |s: &mut Option<&str>| *s = Some(str);
}

Compiler says

let c = |s: &mut Option<&str>| *s = Some(str);
  |                                 ^^^^^^^^^^^^^^ assignment requires that `'1` must outlive `'static`

If I change the code to

fn myfn(str: &str) {
 let mut var = None;
 let c = || var = Some(str);
}

It compiles.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)A-inferenceArea: Type inferenceA-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.T-typesRelevant to the types team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions