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
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//!  This file contains a pair of types
//!  which show how to use closures (Shoes and shoes_in_my_size)
//! and how to create a custom iterator (Counter).

///
/// Shoe 
/// 
///    Struct that captures  a shoe size and style. 
///
#[derive(PartialEq, Debug)]
pub struct Shoe {
    size: u32,
    style: String,              // Unimportant for this.
}
///
/// shoes_in_my_size
/// 
///    Given a vector of shoes, and a shoe size, returns a vector
///    of shoes that only contains the shoes that match the size. 
/// 
pub fn shoes_in_my_size(shoes : Vec<Shoe>, shoe_size : u32) -> Vec<Shoe> {
    shoes.into_iter()     //    +-> Env capture of shoe_size.
        .filter(|s| s.size == shoe_size)
        .collect()
}
// Sample a custom iterator that counts 1 -5:.alloc

///
/// 
///    Provides a counter that behaves like an
///    iterator only counting to 5 before it is 
///    fully consumed.
pub struct Counter {
    count : u32
}
impl Counter {
    ///
    /// Constructs a new Counter object. 
    /// 
    /// # Examples
    /// 
    /// ```
    ///  use iterators::Counter;
    ///  let c = Counter::new();
    /// 
    /// ```
    /// 
    pub fn new() -> Counter { 
        Counter {count:0} 
    }
}
impl Iterator for Counter {
    type Item= u32;
    ///
    /// Returns the next value from a Counter
    /// 
    /// # Examples
    ///
    ///  ```
    /// 
    /// use iterators::Counter;
    /// let mut a = Counter::new();
    /// 
    /// let value = match a.next() {
    ///    Some(i) => i,
    ///    None => 5
    /// };
    /// assert_eq!(value, 1);
    /// 
    /// ```
    /// Note that this code 'pegs' the iterator at 5
    /// once it's consumed
    /// 
    fn next(&mut self) -> Option<Self::Item> {
        self.count += 1;
        if self.count < 6 {
            Some(self.count)
        } else {
            None
        }
    }
}

#[cfg(test)]
mod tests {
    use super::Shoe;
    use super::Counter;
    use super::shoes_in_my_size;
    #[test]
    fn iter_test() {
        let v = vec![1,2,3];

        let mut v1_iter = v.iter();
        assert_eq!(v1_iter.next(), Some(&1));
        assert_eq!(v1_iter.next(), Some(&2));
        assert_eq!(v1_iter.next(), Some(&3));
        assert_eq!(v1_iter.next(), None);
    }
    // Shoe size demo... using iterators that capture env.alloc


    #[test]
    fn sum_test() {
        let v = vec![3,4,5,6];
        let v_iter = v.iter();
        let c_iter = v_iter.cloned();
        let total :u32 = c_iter.sum();              // Consumes the iterator...
        assert_eq!(total, 3+4+5+6);
        // assert_eq!(v_iter.next(), Some(&3));       // So this is not legal.
    }
    #[test]
    fn map_test() {
        let v = vec![3,4,5];
        
        let v1 : Vec<_> =  v.iter().map(|x|  *x+1).collect();
        let mut v_i = v1.iter();
        assert_eq!(v_i.next(), Some(&4));
        assert_eq!(v_i.next(), Some(&5));
        assert_eq!(v_i.next(), Some(&6));
        assert_eq!(v_i.next(), None);
    }
    #[test]
    fn filter_by_size() {
        let shoes = vec![
            Shoe {size: 10, style: String::from("sneakers")},
            Shoe {size: 11, style: String::from("Oxford")},
            Shoe {size: 10, style: String::from("boot")},
        ];

        let in_my_size = shoes_in_my_size(shoes, 10);
        assert_eq!(in_my_size, vec![
            Shoe {size: 10, style: String::from("sneakers")},
            Shoe {size: 10, style: String::from("boot")},
        ]
        );
    }
    #[test]
    fn counter_test_1() {
        let mut c_i = Counter::new();
        

        assert_eq!(c_i.next(), Some(1));
        assert_eq!(c_i.next(), Some(2));
        assert_eq!(c_i.next(), Some(3));
        assert_eq!(c_i.next(), Some(4));
        assert_eq!(c_i.next(), Some(5));
        assert_eq!(c_i.next(), None);
   }
   #[test]
   fn counter_test_2() {
       let  c = Counter::new();
       let result : Vec<u32> = c.filter(|x| x % 2 == 0).collect();
       assert_eq!(
           result, vec![2, 4]
       );
   }

}