Created
May 21, 2026 23:51
-
-
Save malcolmgreaves/85607d402b5983ed7540570d81afd0e3 to your computer and use it in GitHub Desktop.
How to use `?` in a `.filter()` on an iterator.
This file contains hidden or 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
| 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