Partitioning and mapping as one operation?
⚓ Rust 📅 2025-11-08 👤 surdeus 👁️ 6 let impostor_results = self.do_select(params)?;
// Now separate the good results from the errors.
let (items, errors) : (Vec<_>, Vec<_>) = impostor_results
.into_iter()
.partition(|item: &Result<_,_>| item.is_ok());
let items: Vec<RegionImpostorData> = items.into_iter().map(|item: Result<_,_>| item.ok().unwrap()).collect();
let errors: Vec<Error> = errors.into_iter().map(|item: Result<_,_>| item.err().unwrap()).collect();
if !errors.is_empty() {
log::error!("Impostor download fetch errors: {:?}", errors);
}
let json = serde_json::to_string(&items)?;
do_select returns a Vec<Result<Item, Error>>. It's a database read with some validation.
The .partition will separate the OK and Err results. But what comes out is two vectors of Result items, one all Ok and one all Err. So another pass over the data is necessary to strip the Result struct. Is there a a more elegant way to do this? Can those iterators be chained?
1 post - 1 participant
🏷️ Rust_feed