Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #[derive(Debug)]
- struct Index {
- inner: Vec<Description>
- }
- impl Index {
- fn insert(&mut self, element: &impl Described) {
- let foo = element.foo();
- let bar = element.bar();
- self.inner.push(Description { foo, bar });
- }
- }
- #[derive(Debug)]
- struct Description {
- foo: String,
- bar: String,
- }
- trait Described {
- fn foo(&self) -> String;
- fn bar(&self) -> String;
- }
- impl<'a, T> From<&'a [T]> for Index
- where T: Described
- {
- fn from(vec: &'a [T]) -> Index {
- let mut index = Index { inner: Vec::new() };
- for element in vec {
- index.insert(element);
- }
- index
- }
- }
- struct Other(String, String);
- impl Other {
- fn new(foo: &str, bar: &str) -> Self {
- Other(foo.to_owned(), bar.to_owned())
- }
- }
- impl Described for Other {
- fn foo(&self) -> String {
- self.0.clone()
- }
- fn bar(&self) -> String {
- self.1.clone()
- }
- }
- fn main() {
- let others = vec![
- Other::new("a", "b"),
- Other::new("c", "d"),
- ];
- let index = Index::from(others.as_slice());
- println!("{:#?}", index);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement