Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save Ad1n/063c711923cb54331a7f606f9a040c16 to your computer and use it in GitHub Desktop.

Select an option

Save Ad1n/063c711923cb54331a7f606f9a040c16 to your computer and use it in GitHub Desktop.
All combinations between unknown counts of vectors, ordered by exposed list of vectors
fn main() {
let mut v: Vec<Vec<&str>> = Vec::new();
v.push(["1", "2"].to_vec());
v.push(["3", "4", "5"].to_vec());
v.push(["6", "7"].to_vec());
let mut acc = Vec::new();
v.iter()
.map(|el| {
if acc.is_empty() {
for i in el {
println!("empty");
acc.push(Vec::from([i]))
}
} else {
let temp = acc.clone();
acc.clear();
println!("any");
for el1 in temp {
for el2 in el {
let mut t_v = el1.clone();
t_v.push(el2);
acc.push(t_v);
}
}
}
})
.collect::<Vec<_>>();
println!("{:?}", acc);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment