I'm looking for the equivalent of futures-await
's #[async]
for loops.
In futures-await
it looks as follows:
#[async]
for message in stream {
// ...
}
I tried to write the equivalent using Futures 0.3 with edition="2018":
#![feature(futures_api, pin, async_await, await_macro, arbitrary_self_types)]
use futures::{stream, future};
async fn hello() {
let mut my_stream = stream::once(future::ready(17));
async for s in my_stream {
println!("One stream element!");
}
}
My Cargo.toml:
[package]
name = "check_async_method"
version = "0.1.0"
authors = ["real"]
edition = "2018"
[dependencies]
futures-preview = "0.3.0-alpha.7"
As a result, I get this compilation error:
$ cargo run
Compiling check_async_method v0.1.0 (/home/real/temp/check_async_method)
error: expected one of `move`, `|`, or `||`, found `for`
--> src/main.rs:7:11
|
7 | async for s in my_stream {
| ^^^ expected one of `move`, `|`, or `||` here
error: aborting due to previous error
Which probably means that the compiler doesn't understand the combination of the async
and for
keywords. I also tried omitting the async
keyword, but no luck there (:
I assume that I can use the for_each
combinator, but I was wondering if there is a solution similar to what I used with futures-await
.
Any help is appreciated!