In the following snippet, is there a way to have the second example compile without the extra allocation (i.e. without str.to_owned()
), or can I annotate the closure in some way?
How come filter_map
can deal with the reference lifetime here and filter
can't?
use futures::prelude::*;
#[tokio::main]
async fn main() {
let stream = stream::iter(vec!["foo", "bar"]);
// Works fine:
let stream = stream.filter_map(|str| async move {
println!("{}", str);
Some(str)
});
// Fails with:
// return type of closure `impl futures::Future` contains a lifetime `'2`
let stream = stream.filter(|str| {
//let str = str.to_owned();
async move {
println!("{}", str);
true
}
});
stream.collect::<String>().await;
}