rust-lang / rust-lang/rust

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

Open
#155,998 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-closures A-higher-ranked A-inference A-lifetimes C-bug needs-triage T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the two minimal examples centered on myfn, comparing the closure that takes s: &mut Option<&str> with the closure that captures var. Trace the compiler's lifetime treatment for these two closure forms; done means the reported inconsistency is explained and the borrow-checking behavior is corrected or documented by regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.