expression

SQLx streaming concept: stop on row failure

A fallible stream can yield successful rows before a later failure. This offline model shows why success must be reported only after processing all rows, not immediately after the first row. It does not simulate driver cancellation or database transaction behavior. No database connection is required by this example. Expected stdout (not executed): complete: false; partial: 2 Common mistake: Publishing the partial sum as a successful complete result.

Formal Syntax & Structure

rust syntaxrust
stream.try_next().await?

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() -> Result<(), Box<dyn std::error::Error>> {    use futures_util::{stream, TryStreamExt};    let mut rows = stream::iter([Ok(2_i32), Err("decode error"), Ok(9)]);    let mut sum = 0;    let complete = loop {        match rows.try_next().await {            Ok(Some(n)) => sum += n,            Ok(None) => break true,            Err(_) => break false,        }    };    println!("complete: {complete}; partial: {sum}");    Ok(())}
Rust Reference