Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::iter::Iterator;
- type Iterable = Vec<String>;
- struct Collection {
- items: Vec<Iterable>,
- }
- impl Collection {
- fn new() -> Self {
- Collection {
- items: Vec::new(),
- }
- }
- fn insert(&mut self, item: Iterable) {
- self.items.push(item);
- }
- fn iter(&self) -> CollectionIter {
- CollectionIter::new(self)
- }
- }
- struct CollectionIter;
- impl CollectionIter {
- fn new(c: &Collection) -> Self {
- CollectionIter {}
- }
- }
- impl Iterator for CollectionIter {
- type Item = String;
- fn next(&mut self) -> Option<Self::Item> {
- None
- }
- }
- fn main() {
- let item1 = vec!["a".to_string(), "b".to_string(), "c".to_string()];
- let item2 = vec!["d".to_string(), "e".to_string(), "f".to_string()];
- let item3 = vec!["g".to_string(), "h".to_string(), "i".to_string()];
- let mut collection = Collection::new();
- collection.insert(item1);
- collection.insert(item2);
- collection.insert(item3);
- // Should print:
- // 0: a
- // 0: b
- // 0: c
- // 1: d
- // 1: e
- // 1: f
- // 2: g
- // 2: h
- // 2: i
- for i in collection.iter() {
- println!("{}", i);
- }
- }
Add Comment
Please, Sign In to add comment