Created
November 8, 2021 00:32
-
-
Save leaysgur/b654e0e5004799e378a42db9b2f40af5 to your computer and use it in GitHub Desktop.
[Rust] all subsets in vec
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
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