expression

Tonic: handle an error after a successful stream item

A streaming call can produce useful messages before terminating with an error. This synthetic stream of Results models the consumer pattern. It is not a real tonic::Streaming instance. Do not retry blindly after partial side effects. Run in the accompanying generated-bindings Cargo project. Expected stdout (not executed): item 7 unavailable: true Common mistake: Assuming a successful response header guarantees the entire stream completes.

Formal Syntax & Structure

rust syntaxrust
while let Some(item) = stream.next().await { match item { ... } }

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    use tonic::{Status, Code};    use tokio_stream::{self as stream, StreamExt};    let mut items = stream::iter([Ok(7_u64), Err(Status::unavailable("demo failure"))]);    while let Some(item) = items.next().await {        match item {            Ok(id) => println!("item {id}"),            Err(status) => { println!("unavailable: {}", status.code() == Code::Unavailable); break; }        }    }}
Rust Reference