Product in async_std::stream - Rust (original) (raw)

pub trait Product<A = Self>: Sized {
    // Required method
    fn product<'a, S>(stream: S) -> Pin<Box<dyn Future<Output = Self> + 'a>>
       where S: Stream<Item = A> + 'a;
}

Available on unstable only.

Expand description

Trait to represent types that can be created by multiplying the elements of a stream.

This trait is used to implement the product method on streams. Types which implement the trait can be generated by the product method. LikeFromStream this trait should rarely be called directly and instead interacted with through Stream::product.

Source

Method which takes a stream and generates Self from the elements by multiplying the items.

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Source§

Takes each element in the Stream: if it is a None, no further elements are taken, and the None is returned. Should no None occur, the product of all elements is returned.

§Examples

This multiplies every integer in a vector, rejecting the product if a negative element is encountered:

use async_std::prelude::*;
use async_std::stream;

let v = stream::from_iter(vec![1, 2, 4]);
let prod: Option<i32> = v.map(|x|
    if x < 0 {
        None
    } else {
        Some(x)
    }).product().await;
assert_eq!(prod, Some(8));

Source§

Source§

Takes each element in the Stream: if it is an Err, no further elements are taken, and the Err is returned. Should no Err occur, the product of all elements is returned.

§Examples

This multiplies every integer in a vector, rejecting the product if a negative element is encountered:

use async_std::prelude::*;
use async_std::stream;

let v = stream::from_iter(vec![1, 2, 4]);
let res: Result<i32, &'static str> = v.map(|x|
    if x < 0 {
        Err("Negative element found")
    } else {
        Ok(x)
    }).product().await;
assert_eq!(res, Ok(8));