Created
January 21, 2018 16:54
-
-
Save anonymous/ba6ac691e73ee9082d2c786878f86fdc to your computer and use it in GitHub Desktop.
Rust code shared from the playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| trait AsRecord { | |
| fn as_record(&self) -> String; | |
| } | |
| #[derive(Debug)] | |
| struct MyRecord(u32); | |
| impl AsRecord for MyRecord { | |
| fn as_record(&self) -> String { | |
| format!("hey, I've got {}", self.0) | |
| } | |
| } | |
| #[derive(Debug)] | |
| struct Thing { | |
| items: Vec<MyRecord>, | |
| } | |
| impl IntoIterator for Thing { | |
| type Item = MyRecord; | |
| type IntoIter = ::std::vec::IntoIter<MyRecord>; | |
| fn into_iter(self) -> Self::IntoIter { | |
| self.items.into_iter() | |
| } | |
| } | |
| fn do_things<T>(input: T) | |
| where | |
| T: IntoIterator, | |
| T::Item: AsRecord, | |
| { | |
| for item in input { | |
| println!("{}", item.as_record()) | |
| } | |
| } | |
| fn main() { | |
| let thing = Thing { | |
| items: vec![MyRecord(1), MyRecord(32)], | |
| }; | |
| println!("{:?}", thing); | |
| do_things(thing); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment