Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #[derive(Debug)]
  2. struct TestStruct {
  3. value: String,
  4. number: i32,
  5. }
  6.  
  7. #[derive(Debug)]
  8. struct OtherStruct {
  9. number: i32,
  10. other_number: i32,
  11. }
  12.  
  13. impl TestStruct {
  14. fn to_single_item_list(self) -> Vec<Self> {
  15. let mut list = Vec::new();
  16. list.push(self);
  17. list
  18. }
  19. }
  20.  
  21. /*
  22.  
  23. trait SingleItemListTrait: Sized {
  24. fn to_single_item_list(self) -> Vec<Self>;
  25. }
  26.  
  27. impl SingleItemListTrait for TestStruct {
  28. fn to_single_item_list(self) -> Vec<Self> {
  29. let mut list = Vec::new();
  30. list.push(self);
  31. list
  32. }
  33. }
  34.  
  35. impl SingleItemListTrait for OtherStruct {
  36. fn to_single_item_list(self) -> Vec<Self> {
  37. let mut list = Vec::new();
  38. list.push(self);
  39. list
  40. }
  41. }
  42.  
  43. */
  44.  
  45. fn main() {
  46. let test = TestStruct {
  47. value: "MY VALUE".to_owned(),
  48. number: 3,
  49. };
  50. println!("ITEM: {:?}", &test);
  51. let mut list = test.to_single_item_list();
  52. list.push(TestStruct {
  53. value: "VALUE 2".to_owned(),
  54. number: -54,
  55. });
  56. println!("LIST: {:?}", &list);
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement