Skip to content

Instantly share code, notes, and snippets.

@leaysgur
Created November 8, 2021 00:32
Show Gist options
  • Save leaysgur/b654e0e5004799e378a42db9b2f40af5 to your computer and use it in GitHub Desktop.
Save leaysgur/b654e0e5004799e378a42db9b2f40af5 to your computer and use it in GitHub Desktop.
[Rust] all subsets in vec
fn powerset<T>(s: &[T]) -> Vec<Vec<&T>> {
(0..2usize.pow(s.len() as u32))
.map(|i| {
s.iter()
.enumerate()
.filter(|&(t, _)| (i >> t) % 2 == 1)
.map(|(_, e)| e)
.collect()
})
.collect()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment