[Rust] tokio: Stream
Rust에서 Stream은 Iterator 스타일의 비동기 처리 기법을 말한다.
원리나 사용법이나 iterator와 별로 다를건 없다.
여기서는 tokio에서 tokio-stream을 통해 stream을 다루는 방법을 간략히 정리해본다.
tokio-stream은 사실상의 tokio 자체 기능인데, 1.0 출시를 따라오지 못해서 별도 crate로 분리된 비운의... 그거다.
먼저 디펜던시를 추가해준다.

그러면 다음과 같이 stream을 정의하고, 그걸 async/await으로 순서대로 받아오면서 처리를 할 수 있다.
use tokio_stream::StreamExt;
#[tokio::main]
async fn main() {
let mut stream = tokio_stream::iter(&[1, 2, 3]);
while let Some(v) = stream.next().await {
println!("GOT = {:?}", v);
}
}
지속적으로 I/O를 통해 값을 순차적으로 가져오는 로직에 적용하기 좋다.
참조
https://rust-lang.github.io/async-book/05_streams/01_chapter.html
https://samsartor.com/guis-3/