1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//! The Iterator design pattern example module
//!
//! A custom container is instantiated (it already contains hard-coded data
//! to iterate over).  The custom container can then deliver three
//! iterators, each providing a different aspect of the hard-coded data.
//!
//! Accessed through the iterator_exercise() function.

//-----------------------------------------------------------------------------

pub mod iterator_iiterator_trait;
pub mod iterator_iterators;

//-----------------------------------------------------------------------------

use iterator_iiterator_trait::IIterator;
use iterator_iterators::Items;

//-----------------------------------------------------------------------------

/// Example of using the "Iterator" design pattern.
/// 
/// A custom container is instantiated (it already contains hard-coded data
/// to iterate over).  The custom container can then deliver three
/// iterators, each providing a different aspect of the hard-coded data.
/// 
/// The output shows the output from each iterator.
// ! [Using Iterator in Rust]
pub fn iterator_exercise() -> Result<(), String> {
    println!("");
    println!("Iterator Exercise");

    // For this example, the class already has built into it the data
    // to be iterated over.

    // Instantiate the container to be iterated over.
    let items = Items::new();
 
    println!("  Iterating over keys only:");
    let mut key_iterator = items.get_keys();
    loop {
        match key_iterator.next() {
            Some(key) => println!("    {key}"),
            None => break,
        }
    }

    println!("  Iterating over values only:");
    let mut value_iterator = items.get_values();
    loop {
        match value_iterator.next() {
            Some(value) => println!("    {value}"),
            None => break,
        }
    }

    println!("  Iterating over all items:");
    let mut item_iterator = items.get_items();
    loop {
        match item_iterator.next() {
            Some(item) => println!("    {} = {}", item.key, item.value),
            None => break,
        }
    }

    println!("  Done.");

    Ok(())
}
// ! [Using Iterator in Rust]