Skip to content

Non-sense diagnostic message from lifetime checker #67577

Closed
@Mingun

Description

@Mingun

I try to write method, that will return some iterator to filtered internal vector of strings

struct List {
  data: Vec<String>,
}
impl List {
  fn started_with<'a>(&'a self, prefix: &'a str) -> impl Iterator<Item=&'a str> {
    self.data.iter().filter(|s| s.starts_with(prefix)).map(|s| s.as_ref())
  }
}

But this code is not compiling with non-sense error, that recommends me to do thing, that already done:

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0597]: `prefix` does not live long enough
 --> src/lib.rs:6:47
  |
5 |   fn started_with<'a>(&'a self, prefix: &'a str) -> impl Iterator<Item=&'a str> {
  |                   -- lifetime `'a` defined here     --------------------------- opaque type requires that `prefix` is borrowed for `'a`
6 |     self.data.iter().filter(|s| s.starts_with(prefix)).map(|s| s.as_ref())
  |                             ---               ^^^^^^ borrowed value does not live long enough
  |                             |
  |                             value captured here
7 |   }
  |   - `prefix` dropped here while still borrowed

error: aborting due to previous error

For more information about this error, try `rustc --explain E0597`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

Reproduced on all channels

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-borrow-checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler 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