Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug)]
- struct TestStruct {
- value: String,
- number: i32,
- }
- #[derive(Debug)]
- struct OtherStruct {
- number: i32,
- other_number: i32,
- }
- impl TestStruct {
- fn to_single_item_list(self) -> Vec<Self> {
- let mut list = Vec::new();
- list.push(self);
- list
- }
- }
- /*
- trait SingleItemListTrait: Sized {
- fn to_single_item_list(self) -> Vec<Self>;
- }
- impl SingleItemListTrait for TestStruct {
- fn to_single_item_list(self) -> Vec<Self> {
- let mut list = Vec::new();
- list.push(self);
- list
- }
- }
- impl SingleItemListTrait for OtherStruct {
- fn to_single_item_list(self) -> Vec<Self> {
- let mut list = Vec::new();
- list.push(self);
- list
- }
- }
- */
- fn main() {
- let test = TestStruct {
- value: "MY VALUE".to_owned(),
- number: 3,
- };
- println!("ITEM: {:?}", &test);
- let mut list = test.to_single_item_list();
- list.push(TestStruct {
- value: "VALUE 2".to_owned(),
- number: -54,
- });
- println!("LIST: {:?}", &list);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement