Skip to content

Instantly share code, notes, and snippets.

@henninglive
Created September 27, 2017 16:10
Show Gist options
  • Select an option

  • Save henninglive/cdf236b4bf4cb2839557e96f3c3869a2 to your computer and use it in GitHub Desktop.

Select an option

Save henninglive/cdf236b4bf4cb2839557e96f3c3869a2 to your computer and use it in GitHub Desktop.
All possible ways to slice a slice
fn all_splits<T>(s: &[T]) -> Vec<Vec<&[T]>> {
let len = s.len();
let mut vv = Vec::with_capacity(1<<(len - 1));
for mask in 0usize..(1<<(len - 1)) {
let mut v = Vec::with_capacity(mask.count_ones() as usize + 1);
let mut prev = 0;
for i in 1..len {
if (mask >> (i - 1)) & 0b1 != 0 {
v.push(&s[prev..i]);
prev = i;
}
}
v.push(&s[prev..len]);
vv.push(v);
}
vv
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment