@@ -3032,7 +3032,52 @@ impl<A: Clone> DoubleEndedIterator for Repeat<A> {
3032
3032
fn next_back ( & mut self ) -> Option < A > { Some ( self . element . clone ( ) ) }
3033
3033
}
3034
3034
3035
- /// Creates a new iterator that endlessly repeats the element `elt`.
3035
+ /// Creates a new iterator that endlessly repeats a single element.
3036
+ ///
3037
+ /// The `repeat()` function repeats a single value over and over and over and
3038
+ /// over and over and 🔁.
3039
+ ///
3040
+ /// Infinite iterators like `repeat()` are often used with adapters like
3041
+ /// [`take()`], in order to make them finite.
3042
+ ///
3043
+ /// [`take()`]: trait.Iterator.html#method.take
3044
+ ///
3045
+ /// # Examples
3046
+ ///
3047
+ /// Basic usage:
3048
+ ///
3049
+ /// ```
3050
+ /// use std::iter;
3051
+ ///
3052
+ /// // the number four 4ever:
3053
+ /// let mut fours = iter::repeat(4);
3054
+ ///
3055
+ /// assert_eq!(Some(4), fours.next());
3056
+ /// assert_eq!(Some(4), fours.next());
3057
+ /// assert_eq!(Some(4), fours.next());
3058
+ /// assert_eq!(Some(4), fours.next());
3059
+ /// assert_eq!(Some(4), fours.next());
3060
+ ///
3061
+ /// // yup, still four
3062
+ /// assert_eq!(Some(4), fours.next());
3063
+ /// ```
3064
+ ///
3065
+ /// Going finite with [`take()`]:
3066
+ ///
3067
+ /// ```
3068
+ /// use std::iter;
3069
+ ///
3070
+ /// // that last example was too many fours. Let's only have four fours.
3071
+ /// let mut four_fours = iter::repeat(4).take(4);
3072
+ ///
3073
+ /// assert_eq!(Some(4), four_fours.next());
3074
+ /// assert_eq!(Some(4), four_fours.next());
3075
+ /// assert_eq!(Some(4), four_fours.next());
3076
+ /// assert_eq!(Some(4), four_fours.next());
3077
+ ///
3078
+ /// // ... and now we're done
3079
+ /// assert_eq!(None, four_fours.next());
3080
+ /// ```
3036
3081
#[ inline]
3037
3082
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
3038
3083
pub fn repeat < T : Clone > ( elt : T ) -> Repeat < T > {
@@ -3089,6 +3134,19 @@ impl<T> Default for Empty<T> {
3089
3134
}
3090
3135
3091
3136
/// Creates an iterator that yields nothing.
3137
+ ///
3138
+ /// # Exampes
3139
+ ///
3140
+ /// Basic usage:
3141
+ ///
3142
+ /// ```
3143
+ /// use std::iter;
3144
+ ///
3145
+ /// // this could have been an iterator over i32, but alas, it's just not.
3146
+ /// let mut nope = iter::empty::<i32>();
3147
+ ///
3148
+ /// assert_eq!(None, nope.next());
3149
+ /// ```
3092
3150
#[ stable( feature = "iter_empty" , since = "1.2.0" ) ]
3093
3151
pub fn empty < T > ( ) -> Empty < T > {
3094
3152
Empty ( marker:: PhantomData )
@@ -3129,6 +3187,56 @@ impl<T> ExactSizeIterator for Once<T> {
3129
3187
}
3130
3188
3131
3189
/// Creates an iterator that yields an element exactly once.
3190
+ ///
3191
+ /// This is commonly used to adapt a single value into a [`chain()`] of other
3192
+ /// kinds of iteration. Maybe you have an iterator that covers almost
3193
+ /// everything, but you need an extra special case. Maybe you have a function
3194
+ /// which works on iterators, but you only need to process one value.
3195
+ ///
3196
+ /// [`chain()`]: trait.Iterator.html#method.chain
3197
+ ///
3198
+ /// # Examples
3199
+ ///
3200
+ /// Basic usage:
3201
+ ///
3202
+ /// ```
3203
+ /// use std::iter;
3204
+ ///
3205
+ /// // one is the loneliest number
3206
+ /// let mut one = iter::once(1);
3207
+ ///
3208
+ /// assert_eq!(Some(1), one.next());
3209
+ ///
3210
+ /// // just one, that's all we get
3211
+ /// assert_eq!(None, one.next());
3212
+ /// ```
3213
+ ///
3214
+ /// Chaining together with another iterator. Let's say that we want to iterate
3215
+ /// over each file of the `.foo` directory, but also a configuration file,
3216
+ /// `.foorc`:
3217
+ ///
3218
+ /// ```no_run
3219
+ /// use std::iter;
3220
+ /// use std::fs;
3221
+ /// use std::path::PathBuf;
3222
+ ///
3223
+ /// let dirs = fs::read_dir(".foo").unwrap();
3224
+ ///
3225
+ /// // we need to convert from an iterator of DirEntry-s to an iterator of
3226
+ /// // PathBufs, so we use map
3227
+ /// let dirs = dirs.map(|file| file.unwrap().path());
3228
+ ///
3229
+ /// // now, our iterator just for our config file
3230
+ /// let config = iter::once(PathBuf::from(".foorc"));
3231
+ ///
3232
+ /// // chain the two iterators together into one big iterator
3233
+ /// let files = dirs.chain(config);
3234
+ ///
3235
+ /// // this will give us all of the files in .foo as well as .foorc
3236
+ /// for f in files {
3237
+ /// println!("{:?}", f);
3238
+ /// }
3239
+ /// ```
3132
3240
#[ stable( feature = "iter_once" , since = "1.2.0" ) ]
3133
3241
pub fn once < T > ( value : T ) -> Once < T > {
3134
3242
Once { inner : Some ( value) . into_iter ( ) }
0 commit comments