Skip to content

Instantly share code, notes, and snippets.

@ivan
Last active April 27, 2021 19:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivan/9b81127a0781ad9f117ea20e18e4ba18 to your computer and use it in GitHub Desktop.
Save ivan/9b81127a0781ad9f117ea20e18e4ba18 to your computer and use it in GitHub Desktop.
Python generators vs Rust streams
[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"] }
def foo():
for i in range(10):
yield i
for i in foo():
print(i)
#![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