1 // Copyright 2013-2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
15 use option::Option::{self, Some, None};
18 use super::{DoubleEndedIterator, IntoIterator, Iterator, ExactSizeIterator, FusedIterator};
20 /// An iterator that repeats an element endlessly.
22 /// This `struct` is created by the [`repeat()`] function. See its documentation for more.
24 /// [`repeat()`]: fn.repeat.html
25 #[derive(Clone, Debug)]
26 #[stable(feature = "rust1", since = "1.0.0")]
27 pub struct Repeat<A> {
31 #[stable(feature = "rust1", since = "1.0.0")]
32 impl<A: Clone> Iterator for Repeat<A> {
36 fn next(&mut self) -> Option<A> { Some(self.element.clone()) }
38 fn size_hint(&self) -> (usize, Option<usize>) { (usize::MAX, None) }
41 #[stable(feature = "rust1", since = "1.0.0")]
42 impl<A: Clone> DoubleEndedIterator for Repeat<A> {
44 fn next_back(&mut self) -> Option<A> { Some(self.element.clone()) }
47 #[unstable(feature = "fused", issue = "35602")]
48 impl<A: Clone> FusedIterator for Repeat<A> {}
50 /// Creates a new iterator that endlessly repeats a single element.
52 /// The `repeat()` function repeats a single value over and over and over and
53 /// over and over and 🔁.
55 /// Infinite iterators like `repeat()` are often used with adapters like
56 /// [`take()`], in order to make them finite.
58 /// [`take()`]: trait.Iterator.html#method.take
67 /// // the number four 4ever:
68 /// let mut fours = iter::repeat(4);
70 /// assert_eq!(Some(4), fours.next());
71 /// assert_eq!(Some(4), fours.next());
72 /// assert_eq!(Some(4), fours.next());
73 /// assert_eq!(Some(4), fours.next());
74 /// assert_eq!(Some(4), fours.next());
76 /// // yup, still four
77 /// assert_eq!(Some(4), fours.next());
80 /// Going finite with [`take()`]:
85 /// // that last example was too many fours. Let's only have four fours.
86 /// let mut four_fours = iter::repeat(4).take(4);
88 /// assert_eq!(Some(4), four_fours.next());
89 /// assert_eq!(Some(4), four_fours.next());
90 /// assert_eq!(Some(4), four_fours.next());
91 /// assert_eq!(Some(4), four_fours.next());
93 /// // ... and now we're done
94 /// assert_eq!(None, four_fours.next());
97 #[stable(feature = "rust1", since = "1.0.0")]
98 pub fn repeat<T: Clone>(elt: T) -> Repeat<T> {
102 /// An iterator that yields nothing.
104 /// This `struct` is created by the [`empty()`] function. See its documentation for more.
106 /// [`empty()`]: fn.empty.html
107 #[stable(feature = "iter_empty", since = "1.2.0")]
108 pub struct Empty<T>(marker::PhantomData<T>);
110 #[stable(feature = "core_impl_debug", since = "1.9.0")]
111 impl<T> fmt::Debug for Empty<T> {
112 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
117 #[stable(feature = "iter_empty", since = "1.2.0")]
118 impl<T> Iterator for Empty<T> {
121 fn next(&mut self) -> Option<T> {
125 fn size_hint(&self) -> (usize, Option<usize>){
130 #[stable(feature = "iter_empty", since = "1.2.0")]
131 impl<T> DoubleEndedIterator for Empty<T> {
132 fn next_back(&mut self) -> Option<T> {
137 #[stable(feature = "iter_empty", since = "1.2.0")]
138 impl<T> ExactSizeIterator for Empty<T> {
139 fn len(&self) -> usize {
144 #[unstable(feature = "fused", issue = "35602")]
145 impl<T> FusedIterator for Empty<T> {}
147 // not #[derive] because that adds a Clone bound on T,
148 // which isn't necessary.
149 #[stable(feature = "iter_empty", since = "1.2.0")]
150 impl<T> Clone for Empty<T> {
151 fn clone(&self) -> Empty<T> {
152 Empty(marker::PhantomData)
156 // not #[derive] because that adds a Default bound on T,
157 // which isn't necessary.
158 #[stable(feature = "iter_empty", since = "1.2.0")]
159 impl<T> Default for Empty<T> {
160 fn default() -> Empty<T> {
161 Empty(marker::PhantomData)
165 /// Creates an iterator that yields nothing.
174 /// // this could have been an iterator over i32, but alas, it's just not.
175 /// let mut nope = iter::empty::<i32>();
177 /// assert_eq!(None, nope.next());
179 #[stable(feature = "iter_empty", since = "1.2.0")]
180 pub fn empty<T>() -> Empty<T> {
181 Empty(marker::PhantomData)
184 /// An iterator that yields an element exactly once.
186 /// This `struct` is created by the [`once()`] function. See its documentation for more.
188 /// [`once()`]: fn.once.html
189 #[derive(Clone, Debug)]
190 #[stable(feature = "iter_once", since = "1.2.0")]
192 inner: ::option::IntoIter<T>
195 #[stable(feature = "iter_once", since = "1.2.0")]
196 impl<T> Iterator for Once<T> {
199 fn next(&mut self) -> Option<T> {
203 fn size_hint(&self) -> (usize, Option<usize>) {
204 self.inner.size_hint()
208 #[stable(feature = "iter_once", since = "1.2.0")]
209 impl<T> DoubleEndedIterator for Once<T> {
210 fn next_back(&mut self) -> Option<T> {
211 self.inner.next_back()
215 #[stable(feature = "iter_once", since = "1.2.0")]
216 impl<T> ExactSizeIterator for Once<T> {
217 fn len(&self) -> usize {
222 #[unstable(feature = "fused", issue = "35602")]
223 impl<T> FusedIterator for Once<T> {}
225 /// Creates an iterator that yields an element exactly once.
227 /// This is commonly used to adapt a single value into a [`chain()`] of other
228 /// kinds of iteration. Maybe you have an iterator that covers almost
229 /// everything, but you need an extra special case. Maybe you have a function
230 /// which works on iterators, but you only need to process one value.
232 /// [`chain()`]: trait.Iterator.html#method.chain
241 /// // one is the loneliest number
242 /// let mut one = iter::once(1);
244 /// assert_eq!(Some(1), one.next());
246 /// // just one, that's all we get
247 /// assert_eq!(None, one.next());
250 /// Chaining together with another iterator. Let's say that we want to iterate
251 /// over each file of the `.foo` directory, but also a configuration file,
257 /// use std::path::PathBuf;
259 /// let dirs = fs::read_dir(".foo").unwrap();
261 /// // we need to convert from an iterator of DirEntry-s to an iterator of
262 /// // PathBufs, so we use map
263 /// let dirs = dirs.map(|file| file.unwrap().path());
265 /// // now, our iterator just for our config file
266 /// let config = iter::once(PathBuf::from(".foorc"));
268 /// // chain the two iterators together into one big iterator
269 /// let files = dirs.chain(config);
271 /// // this will give us all of the files in .foo as well as .foorc
273 /// println!("{:?}", f);
276 #[stable(feature = "iter_once", since = "1.2.0")]
277 pub fn once<T>(value: T) -> Once<T> {
278 Once { inner: Some(value).into_iter() }