Skip to content

Instantly share code, notes, and snippets.

@wulfgarpro
Last active January 11, 2023 08:21
Show Gist options
  • Save wulfgarpro/00dba9df304e7c170700732a2f424933 to your computer and use it in GitHub Desktop.
Save wulfgarpro/00dba9df304e7c170700732a2f424933 to your computer and use it in GitHub Desktop.
Stop iterator when error is encountered, and report.
use anyhow::{anyhow, Result};
fn main() -> Result<()> {
let v = vec![false, true, true];
let res = v.iter().map(|x| {
println!("looped"); // Loops only two times, not 3.
if *x {
Err(anyhow!("error"))
} else {
Ok(())
}
}).collect();
res
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment