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

/// Return an iterator with `n` elements, for simple repetition
/// a particular number of times. The iterator yields a counter.
///
/// Iterator element type is `usize`
#[inline]
pub fn times(n: usize) -> Times
{
    Times{i: 0, n: n}
}

/// A simple iterator to repeat a given number of times
///
/// Created with the `times()` function.
///
/// Iterator element type is `usize`
#[derive(Copy, Clone)]
pub struct Times {
    i: usize,
    n: usize,
}

impl Iterator for Times
{
    type Item = usize;
    #[inline]
    fn next(&mut self) -> Option<usize>
    {
        let elt = self.i;
        if self.i < self.n {
            self.i += 1;
            Some(elt)
        } else { None }
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>)
    {
        let len = self.n - self.i;
        (len, Some(len))
    }
}

impl DoubleEndedIterator for Times
{
    #[inline]
    fn next_back(&mut self) -> Option<usize>
    {
        if self.i < self.n {
            self.n -= 1;
            Some(self.n)
        } else { None }
    }
}

impl ExactSizeIterator for Times { }