Reputation: 1490
I'm trying to get the following to compile:
fn read<'a>(tokens: impl Iterator<Item=&'a str>) -> impl Iterator<Item=String> {
tokens.map(|t| t.to_owned())
}
error[E0482]: lifetime of return value does not outlive the function call
--> src/lib.rs:1:53
|
1 | fn read<'a>(tokens: impl Iterator<Item=&'a str>) -> impl Iterator<Item=String> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: the return value is only valid for the lifetime `'a` as defined on the function body at 1:9
--> src/lib.rs:1:9
|
1 | fn read<'a>(tokens: impl Iterator<Item=&'a str>) -> impl Iterator<Item=String> {
| ^^
Playground link: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=55460a8b17ff10e30b56a9142338ec19
What's strange to me is that the return value is concerned with lifetimes at all; its contents are owned and passed directly outward.
I came across this blog post: https://blog.katona.me/2019/12/29/Rust-Lifetimes-and-Iterators/
but neither of these worked:
fn read<'a>(tokens: impl Iterator<Item=&'a str>) -> impl Iterator<Item=String> + '_ {
fn read<'a>(tokens: impl Iterator<Item=&'a str>) -> impl Iterator<Item=String> + 'a {
I suspect I'm misunderstanding some key aspect of Iterators here. Any suggestions?
Upvotes: 3
Views: 311
Reputation: 1490
I solved it by adding + 'a
to both Iterators:
fn read<'a>(tokens: impl Iterator<Item=&'a str> + 'a) -> impl Iterator<Item=String> + 'a {
tokens.map(|t| t.to_owned())
}
Upvotes: 2