我正在尝试将 rust future 流(重复)连接到水槽(排水)。
第一种方法可以正常工作(请在下面检查),但是第二种方法使用.forward无法编译(内联显示错误)。关于我在做什么错的任何指示?

use futures::stream::StreamExt;
use futures::sink::SinkExt;
use futures::executor;

fn main() {
    executor::block_on( async {
        // works
        let mut work = futures::stream::repeat(5);
        let mut drain = futures::sink::drain();
        match drain.send(work.next()).await {
            Ok(_r) => println!("Done"),
            Err(_e) => println!("Error"),
        };

        // works
        let res: Vec<i32> = futures::stream::repeat(5).take(3).collect::<Vec<i32>>().await;
        println!("{:?}", res);

        // doesn't work
        // futures::stream::repeat(5).forward(futures::sink::drain());
        //                            ^^^^^^^ expected integer, found enum `std::result::Result`
        //  error[E0271]: type mismatch resolving `<futures_util::stream::repeat::Repeat<{integer}> as futures_core::stream::Stream>::Item == std::result::Result<_, _>`

        // Fixed above with repeat(Ok(5)) but not repeat(5)
        match futures::stream::repeat(Ok(5)).take(3).forward(futures::sink::drain()).await {
            Ok(_r) => println!("Done"),
            Err(_e) => println!("Error"),
        };
    });
}

相关文件
  • https://docs.rs/futures/0.3.1/futures/stream/fn.repeat.html
  • https://docs.rs/futures/0.3.1/futures/sink/fn.drain.html
  • https://docs.rs/futures/0.3.1/futures/stream/trait.StreamExt.html#method.forward
  • 最佳答案

    我想你想要更多这样的东西吗?

    use futures::stream::StreamExt;
    use futures::sink::SinkExt;
    
    #[tokio::main]
    async fn main() {
        // works
        let mut work = futures::stream::repeat(5);
        let mut drain = futures::sink::drain();
        match drain.send(work.next()).await {
            Ok(_r) => println!("Done"),
            Err(_e) => println!("Error"),
        };
        // doesn't work
    
        futures::stream::repeat(Ok(5)).forward(futures::sink::drain());
    }
    

    正如错误(稍有混淆)所说明的那样,在这种情况下,重复调用必须采用Result参数。

    关于rust - StreamExt::forward失败,错误为 `expected integer, found enum ` std::result::Result``,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59751192/

    10-09 19:40