Skip to content

Instantly share code, notes, and snippets.

@malcolmgreaves
Created May 21, 2026 23:51
Show Gist options
  • Select an option

  • Save malcolmgreaves/85607d402b5983ed7540570d81afd0e3 to your computer and use it in GitHub Desktop.

Select an option

Save malcolmgreaves/85607d402b5983ed7540570d81afd0e3 to your computer and use it in GitHub Desktop.
How to use `?` in a `.filter()` on an iterator.
type Repository;
type Error;
trait X {
fn is_needed(repo: &Repository) -> Result<bool, Error>
}
const ALL: [&dyn X; N] = [...];
let xs_or_error: Result<Vec<&dyn X>, Error> = ALL
.iter()
.map(|migration| -> Result<Option<&dyn Migrate>, OxenError> {
if migration.is_needed(repo)? {
Ok(Some(*migration))
} else {
Ok(None)
}
})
.filter_map(|x| x.transpose())
.collect();
let needed_xs: Vec<&dyn X> = x_or_error?;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment