Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #[derive(Debug)]
  2. struct Index {
  3. inner: Vec<Description>
  4. }
  5.  
  6. impl Index {
  7. fn insert(&mut self, element: Description) {
  8. self.inner.push(element);
  9. }
  10. }
  11.  
  12. #[derive(Debug)]
  13. struct Description {
  14. foo: String,
  15. bar: String,
  16. }
  17.  
  18. impl<'a, T> From<&'a [T]> for Index
  19. where &'a T: Into<Description>
  20. {
  21. fn from(vec: &'a [T]) -> Index {
  22. let mut index = Index { inner: Vec::new() };
  23. for element in vec {
  24. index.insert(element.into());
  25. }
  26. index
  27. }
  28. }
  29.  
  30. struct Other(String, String);
  31.  
  32. impl Other {
  33. fn new(foo: &str, bar: &str) -> Self {
  34. Other(foo.to_owned(), bar.to_owned())
  35. }
  36. }
  37.  
  38. impl From<&Other> for Description {
  39. fn from(other: &Other) -> Description {
  40. Description {
  41. foo: other.0.clone(),
  42. bar: other.1.clone(),
  43. }
  44. }
  45. }
  46.  
  47. fn main() {
  48. let others = vec![
  49. Other::new("a", "b"),
  50. Other::new("c", "d"),
  51. ];
  52. let index = Index::from(others.as_slice());
  53. println!("{:#?}", index);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement