Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. use std::any::Any;
  2.  
  3. pub fn dedup_types(list: &mut Vec<Box<Any>>) {
  4. list.sort_unstable_by_key(|elem| elem.type_id());
  5. list.dedup_by_key(|elem| (*elem).type_id());
  6. }
  7.  
  8. struct Alpha;
  9. struct Beta;
  10. struct Gamma;
  11.  
  12. pub fn main() {
  13. let mut list = vec![
  14. Box::new(Alpha) as Box<Any>,
  15. Box::new(Gamma) as Box<Any>,
  16. Box::new(Alpha) as Box<Any>,
  17. Box::new(Beta) as Box<Any>,
  18. Box::new(Gamma) as Box<Any>,
  19. Box::new(Gamma) as Box<Any>,
  20. ];
  21.  
  22. dedup_types(&mut list);
  23. assert_eq!(list.len(), 3);
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement