Last active
April 27, 2021 19:31
-
-
Save ivan/9b81127a0781ad9f117ea20e18e4ba18 to your computer and use it in GitHub Desktop.
Python generators vs Rust streams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "learn-async-stream" | |
version = "0.1.0" | |
edition = "2018" | |
[dependencies] | |
futures = "0.3" | |
futures-async-stream = "0.2" | |
tokio = { version = "0.2", features = ["full"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def foo(): | |
for i in range(10): | |
yield i | |
for i in foo(): | |
print(i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(generators, proc_macro_hygiene, stmt_expr_attributes)] | |
use futures::stream::Stream; | |
use futures::pin_mut; | |
use futures_async_stream::stream; | |
use tokio::stream::StreamExt; | |
fn foo() -> impl Stream<Item = i32> { | |
#[stream] | |
async move { | |
for i in 0..10 { | |
yield i; | |
} | |
} | |
} | |
#[tokio::main] | |
async fn main() { | |
let stream = foo(); | |
pin_mut!(stream); | |
while let Some(s) = stream.next().await { | |
println!("{}", s); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/taiki-e/futures-async-stream
https://docs.rs/futures-async-stream/0.2.0/futures_async_stream/