Skip to content

Instantly share code, notes, and snippets.

@sam0x17
Created July 26, 2023 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sam0x17/41d1659281096ba6de75f2d2246dc371 to your computer and use it in GitHub Desktop.
Save sam0x17/41d1659281096ba6de75f2d2246dc371 to your computer and use it in GitHub Desktop.
Rust workaround for "the `?` operator can only be used in a closure that returns `Result` or `Option`" when trying to use `?` inside a closure inside a `.map`
let const_fns: Vec<TraitItemFn> = const_fns
.into_iter()
.map(|item| match item {
TraitItem::Fn(trait_item_fn) => Ok(*trait_item_fn),
_ => return Err(Error::new(item.span(), "expected `fn`")),
})
.collect::<std::result::Result<_, Self::Error>>()?;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment