@@ -25,6 +25,7 @@ mod all;
25
25
mod any;
26
26
mod min_by;
27
27
mod next;
28
+ mod nth;
28
29
mod take;
29
30
30
31
pub use take:: Take ;
@@ -33,6 +34,7 @@ use all::AllFuture;
33
34
use any:: AnyFuture ;
34
35
use min_by:: MinByFuture ;
35
36
use next:: NextFuture ;
37
+ use nth:: NthFuture ;
36
38
37
39
use std:: cmp:: Ordering ;
38
40
use std:: marker:: PhantomData ;
@@ -161,6 +163,64 @@ pub trait Stream {
161
163
MinByFuture :: new ( self , compare)
162
164
}
163
165
166
+ /// Returns the nth element of the stream.
167
+ ///
168
+ /// # Examples
169
+ ///
170
+ /// Basic usage:
171
+ ///
172
+ /// ```
173
+ /// # fn main() { async_std::task::block_on(async {
174
+ /// #
175
+ /// use std::collections::VecDeque;
176
+ /// use async_std::stream::Stream;
177
+ ///
178
+ /// let mut s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
179
+ ///
180
+ /// let second = s.nth(1).await;
181
+ /// assert_eq!(second, Some(2));
182
+ /// #
183
+ /// # }) }
184
+ /// ```
185
+ /// Calling `nth()` multiple times:
186
+ ///
187
+ /// ```
188
+ /// # fn main() { async_std::task::block_on(async {
189
+ /// #
190
+ /// use std::collections::VecDeque;
191
+ /// use async_std::stream::Stream;
192
+ ///
193
+ /// let mut s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
194
+ ///
195
+ /// let second = s.nth(0).await;
196
+ /// assert_eq!(second, Some(1));
197
+ ///
198
+ /// let second = s.nth(0).await;
199
+ /// assert_eq!(second, Some(2));
200
+ /// #
201
+ /// # }) }
202
+ /// ```
203
+ /// Returning `None` if the stream finished before returning `n` elements:
204
+ /// ```
205
+ /// # fn main() { async_std::task::block_on(async {
206
+ /// #
207
+ /// use std::collections::VecDeque;
208
+ /// use async_std::stream::Stream;
209
+ ///
210
+ /// let mut s: VecDeque<usize> = vec![1, 2, 3].into_iter().collect();
211
+ ///
212
+ /// let fourth = s.nth(4).await;
213
+ /// assert_eq!(fourth, None);
214
+ /// #
215
+ /// # }) }
216
+ /// ```
217
+ fn nth ( & mut self , n : usize ) -> ret ! ( ' _, NthFuture , Option <Self :: Item >)
218
+ where
219
+ Self : Sized ,
220
+ {
221
+ NthFuture :: new ( self , n)
222
+ }
223
+
164
224
/// Tests if every element of the stream matches a predicate.
165
225
///
166
226
/// `all()` takes a closure that returns `true` or `false`. It applies
0 commit comments