Skip to content

Commit a2d8e61

Browse files
committed
from_fn example
Signed-off-by: Yoshua Wuyts <[email protected]>
1 parent ac4727e commit a2d8e61

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/stream/from_fn.rs

+22
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@ use futures::stream::Stream;
1111
/// This allows creating a custom iterator with any behavior
1212
/// without using the more verbose syntax of creating a dedicated type
1313
/// and implementing the `Iterator` trait for it.
14+
///
15+
/// # Examples
16+
///
17+
/// ```
18+
/// # fn main() { async_std::task::block_on(async {
19+
/// #
20+
/// let mut count = 0;
21+
/// let counter = async_std::stream::from_fn(|| async move {
22+
/// // Increment our count. This is why we started at zero.
23+
/// count += 1;
24+
///
25+
/// // Check to see if we've finished counting or not.
26+
/// if count < 6 {
27+
/// Some(count)
28+
/// } else {
29+
/// None
30+
/// }
31+
/// });
32+
/// assert_eq!(counter.collect::<Vec<_>>().await, &[1, 2, 3, 4, 5]);
33+
/// #
34+
/// # }) }
35+
/// ```
1436
#[inline]
1537
pub fn from_fn<T, F>(f: F) -> FromFn<F, T>
1638
where F: FnMut() -> Box<dyn Future<Output = Option<T>>>,

0 commit comments

Comments
 (0)