Closed
Description
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:
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