理解错误:特质`futures :: future :: Future`没有为(()`实现 [英] understanding error: trait `futures::future::Future` is not implemented for `()`

查看:313
本文介绍了理解错误:特质`futures :: future :: Future`没有为(()`实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是关于如何阅读Rust文档并增进我对Rust的理解,从而了解如何解决这个特定的编译器错误.

This question is about how to read the Rust documentation and improve my understanding of Rust so as to understand how to address this specific compiler error.

我已阅读 tokio文档,并尝试了许多

I've read the tokio docs and experimented with many of the examples. In writing my own code, I frequently run into compiler errors that I don't understand and often found I can fix the code, but don't understand why specific syntax is needed.

我复制了一个基于tokio的 hello world 的非常简单的示例. :

I reproduced with a very simple example based on tokio's hello world:

use futures::Future;
use tokio::net::TcpStream;
use tokio::prelude::*;

fn main() {
  let addr = "127.0.0.1:6142".parse().unwrap();

  let client = TcpStream::connect(&addr).and_then(|stream| {
      println!("created stream");
      // Process stream here.

      // Ok(())
  });

}

上面的代码不正确,需要注释掉的Ok().我知道这是事实,但并非确切原因.这可能是先前问题的另一半

The above code is incorrect, requiring the commented out Ok(). I know that this is true, but not exactly why. This is perhaps the other half of a prior question How do I interpret the signature of read_until and what is AsyncRead + BufRead in Tokio? -- now I understand closures better, but can't quite parse the docs to understand the expected return value.

当我尝试编译上面的错误代码时,出现以下错误:

When I attempt to compile the incorrect code above, I get the following error:

error[E0277]: the trait bound `(): futures::future::Future` is not satisfied
 --> tokio-chat-client/src/main.rs:8:42
  |
8 |   let client = TcpStream::connect(&addr).and_then(|stream| {
  |                                          ^^^^^^^^ the trait `futures::future::Future` is not implemented for `()`
  |
  = note: required because of the requirements on the impl of `futures::future::IntoFuture` for `()`

我的问题有两个部分:

  1. 尝试告诉我的错误消息是什么?
  2. 我如何将文档用于 and_then 了解期望的返回值?
  1. What is the error message trying to tell me?
  2. How would I use the docs for and_then to understand the expected return value?

推荐答案

fn and_then<F, B>(self, f: F) -> AndThen<Self, B, F> where
    F: FnOnce(Self::Item) -> B,
    B: IntoFuture<Error = Self::Error>,
    Self: Sized, 

这意味着:

  • 您的闭包必须接受类型为Self::Item的参数,并返回某些类型为B
  • 闭包返回的类型B必须可以转换为将来的类型.
  • 如果将来返回错误,则该错误必须为Self::Error类型.
  • Your closure must accept an argument of type Self::Item and return some type B
  • The type B returned by your closure must be convertible into a future.
  • And if that future returns an error, then that error must have type Self::Error.

此外,如果您查看 ,您将看到它是

Moreover, if you look at the doc for IntoFuture, you will see that it is implemented for Result, so it works for Ok(()), but that it is not implemented for (), so it doesn't work if your closure returns nothing.

这篇关于理解错误:特质`futures :: future :: Future`没有为(()`实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆