@@ -25,6 +25,7 @@ mod all;
2525mod any;
2626mod min_by;
2727mod next;
28+ mod nth;
2829mod take;
2930
3031pub use take:: Take ;
@@ -33,6 +34,7 @@ use all::AllFuture;
3334use any:: AnyFuture ;
3435use min_by:: MinByFuture ;
3536use next:: NextFuture ;
37+ use nth:: NthFuture ;
3638
3739use std:: cmp:: Ordering ;
3840use std:: marker:: PhantomData ;
@@ -161,6 +163,64 @@ pub trait Stream {
161163 MinByFuture :: new ( self , compare)
162164 }
163165
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+
164224 /// Tests if every element of the stream matches a predicate.
165225 ///
166226 /// `all()` takes a closure that returns `true` or `false`. It applies
0 commit comments