Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. fn main() {
  2. println!("Hello, world!");
  3.  
  4. let list = [1,2,3,4];
  5.  
  6. dbg!(foo(&list, 2));
  7. }
  8.  
  9. fn foo<T: Clone+std::fmt::Debug>(list: &[T], n: usize) -> Vec<Vec<T>> {
  10. let mut ret = Vec::new();
  11.  
  12. if list.len() == n {
  13. ret.push(list.to_vec());
  14. } else if n == 1 {
  15. ret.extend(list.iter().map(|i| vec![i.clone()]));
  16. } else {
  17.  
  18. for first_i in 0..list.len()-n+1 {
  19. let results = foo(&list[first_i+1..],n-1);
  20.  
  21. for mut r in results {
  22. r.push(list[first_i].clone());
  23. ret.push(r);
  24. }
  25. }
  26. }
  27.  
  28. ret
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement