Guest User

Untitled

a guest
Jun 13th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. use std::iter::Iterator;
  2.  
  3. type Iterable = Vec<String>;
  4.  
  5. struct Collection {
  6. items: Vec<Iterable>,
  7. }
  8.  
  9. impl Collection {
  10. fn new() -> Self {
  11. Collection {
  12. items: Vec::new(),
  13. }
  14. }
  15.  
  16. fn insert(&mut self, item: Iterable) {
  17. self.items.push(item);
  18. }
  19.  
  20. fn iter(&self) -> CollectionIter {
  21. CollectionIter::new(self)
  22. }
  23. }
  24.  
  25. struct CollectionIter;
  26.  
  27. impl CollectionIter {
  28. fn new(c: &Collection) -> Self {
  29. CollectionIter {}
  30. }
  31. }
  32.  
  33. impl Iterator for CollectionIter {
  34. type Item = String;
  35.  
  36. fn next(&mut self) -> Option<Self::Item> {
  37. None
  38. }
  39. }
  40.  
  41. fn main() {
  42. let item1 = vec!["a".to_string(), "b".to_string(), "c".to_string()];
  43. let item2 = vec!["d".to_string(), "e".to_string(), "f".to_string()];
  44. let item3 = vec!["g".to_string(), "h".to_string(), "i".to_string()];
  45.  
  46. let mut collection = Collection::new();
  47. collection.insert(item1);
  48. collection.insert(item2);
  49. collection.insert(item3);
  50.  
  51. // Should print:
  52. // 0: a
  53. // 0: b
  54. // 0: c
  55. // 1: d
  56. // 1: e
  57. // 1: f
  58. // 2: g
  59. // 2: h
  60. // 2: i
  61. for i in collection.iter() {
  62. println!("{}", i);
  63. }
  64. }
Add Comment
Please, Sign In to add comment