Skip to content

Instantly share code, notes, and snippets.

Created January 21, 2018 16:54
Show Gist options
  • Select an option

  • Save anonymous/ba6ac691e73ee9082d2c786878f86fdc to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/ba6ac691e73ee9082d2c786878f86fdc to your computer and use it in GitHub Desktop.
Rust code shared from the playground
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