Last active
January 11, 2023 08:21
-
-
Save wulfgarpro/00dba9df304e7c170700732a2f424933 to your computer and use it in GitHub Desktop.
Stop iterator when error is encountered, and report.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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