Skip to content

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

Closed as duplicate

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.

Activity

  1. ghost added
    C-bugCategory: This is a bug.
    on Apr 30, 2026
  2. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Apr 30, 2026
  3. theemathas commented on Apr 30, 2026

    @theemathas
    Contributor

    As a workaround, you can manually annotate the lifetime:

    fn myfn<'a>(str: &'a str) {
        let c = |s: &mut Option<&'a str>| *s = Some(str);
    }
  4. added
    A-lifetimesArea: Lifetimes / regions
    A-closuresArea: Closures (`|…| { … }`)
    T-typesRelevant to the types team, which will review and decide on the PR/issue.
    A-higher-rankedArea: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs)
    on Apr 30, 2026
  5. QuineDot commented on Aug 22, 2026

    @QuineDot

    This is #100002 and perhaps others: a hidden or wildcard lifetime in a closure argument types is always interpreted as "must take any lifetime", instead of "takes one specific lifetime".

    Removing the &str (where the hidden lifetime "is") also works around the problem.

        let c = |s: &mut Option<_>| *s = Some(str);
        let c = |s: &mut _| *s = Some(str);

    Overriding default closure inference with a trait-bound identity function "funnel" also works around the problem.

        fn funnel<'s, F: Fn(&mut Option<&'s str>)>(f: F) -> F { f }
        let c = funnel(|s: &mut Option<&str>| *s = Some(str));
  6. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions