Guest User

Untitled

a guest
Oct 15th, 2019
80
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use itertools::Itertools;
  2.  
  3. #[derive(Debug, PartialEq)]
  4. struct User {
  5. id: i32,
  6. }
  7.  
  8. #[derive(Debug)]
  9. struct Post {
  10. id: i32,
  11. uid: i32,
  12. title: &'static str,
  13. }
  14.  
  15. fn main() {
  16. let query = vec![
  17. (User { id: 1 }, Post { id: 1, uid: 1, title: "a" }),
  18. (User { id: 1 }, Post { id: 2, uid: 1, title: "b" }),
  19. (User { id: 2 }, Post { id: 2, uid: 2, title: "a" }),
  20. ];
  21. let users = query.iter()
  22. .group_by(|(user, _post)| user)
  23. .into_iter()
  24. .map(|(user, group)| {
  25. (user, group.map(|(_user, post)| post).collect::<Vec<&Post>>())
  26. })
  27. .collect::<Vec<_>>();
  28. dbg!(users);
  29. }
RAW Paste Data