Skip to content

NLL error on closure, but not on equivalent function #55526

Closed
@Avi-D-coder

Description

@Avi-D-coder

With the 2018 edition on nightly or #![feature(nll)]:

fn main() {
    let _v_lambda = |v: &Vec<String>| v.iter().chain(v.iter()).collect::<Vec<&String>>();
}

fn _v_fn(v: &Vec<String>) -> Vec<&String> {
    v.iter().chain(v.iter()).collect::<Vec<&String>>()
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
warning: unsatisfied lifetime constraints
 --> src/main.rs:2:39
  |
2 |     let _v_lambda = |v: &Vec<String>| v.iter().chain(v.iter()).collect::<Vec<&String>>();
  |                         -           - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'2`
  |                         |           |
  |                         |           return type of closure is std::vec::Vec<&'2 std::string::String>
  |                         let's call the lifetime of this reference `'1`
  |
  = warning: This error has been downgraded to a warning for backwards compatibility with previous releases.
          It represents potential unsoundness in your code.
          This warning will become a hard error in the future.

    Finished dev [unoptimized + debuginfo] target(s) in 0.55s
     Running `target/debug/playground`

Metadata

Metadata

Assignees

Labels

A-NLLArea: Non-lexical lifetimes (NLL)A-closuresArea: Closures (`|…| { … }`)A-inferenceArea: Type inferenceA-lifetimesArea: Lifetimes / regionsNLL-completeWorking towards the "valid code works" goalP-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language 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