How to use async iterators in Rust

Implement async iterators in Rust using the async-stream crate to yield items lazily without blocking execution.

Use async iterators in Rust by implementing the Stream trait from the futures crate or using the async-stream crate to yield items asynchronously.

use async_stream::stream;

async fn async_numbers() {
    let mut count = 0;
    let s = stream! {
        for i in 0..5 {
            count += 1;
            yield i;
        }
    };
    // Consume the stream
    while let Some(n) = s.next().await {
        println!("Got: {}", n);
    }
}

Install the crate first: cargo add async-stream.