1

我一直在为 rust 中的超级板条箱寻找任何工作示例和好的文档,并在这里遇到了一个示例。但是,当我运行该示例时,我遇到了几个错误。

error[E0061]: this function takes 1 parameter but 0 parameters were supplied
 --> src/main.rs:8:18
  |
8 |     let client = Client::new();
  |                  ^^^^^^^^^^^^^ expected 1 parameter

error[E0308]: mismatched types
  --> src/main.rs:11:26
   |
11 |     let res = client.get("https://www.reddit.com/r/programming/.rss")
   |                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected struct `hyper::Uri`, found reference
   |
   = note: expected type `hyper::Uri`
              found type `&'static str`

error[E0599]: no method named `send` found for type `hyper::client::FutureResponse` in the current scope
  --> src/main.rs:12:22
   |
12 |                     .send()
   |                      ^^^^

error: aborting due to 3 previous errors

有谁知道我做错了什么?当示例代码对我不起作用时,我真的很难理解如何制作功能性 rust 代码。

看起来该示例已过时,因此请从超级文档中获取示例并添加 fn main() 以便它可以工作:

extern crate futures;
extern crate hyper;
extern crate tokio_core;

use std::io::{self, Write};
use futures::{Future, Stream};
use hyper::Client;
use tokio_core::reactor::Core;

fn main(){
    let mut core = Core::new()?;
    let client = Client::new(&core.handle());

    let uri = "http://httpbin.org/ip".parse()?;
    let work = client.get(uri).and_then(|res| {
        println!("Response: {}", res.status());

        res.body().for_each(|chunk| {
            io::stdout()
                .write_all(&chunk)
                .map_err(From::from)
        })
    });
    core.run(work)?;
}

失败并出现错误:

error[E0277]: the trait bound `(): std::ops::Try` is not satisfied
  --> src/main.rs:11:20
   |
11 |     let mut core = Core::new()?;
   |                    ------------
   |                    |
   |                    the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)
   |                    in this macro invocation
   |
   = help: the trait `std::ops::Try` is not implemented for `()`
   = note: required by `std::ops::Try::from_error`

error[E0277]: the trait bound `(): std::ops::Try` is not satisfied
  --> src/main.rs:14:15
   |
14 |     let uri = "http://httpbin.org/ip".parse()?;
   |               --------------------------------
   |               |
   |               the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)
   |               in this macro invocation
   |
   = help: the trait `std::ops::Try` is not implemented for `()`
   = note: required by `std::ops::Try::from_error`

error[E0277]: the trait bound `(): std::ops::Try` is not satisfied
  --> src/main.rs:24:5
   |
24 |     core.run(work)?;
   |     ---------------
   |     |
   |     the `?` operator can only be used in a function that returns `Result` (or another type that implements `std::ops::Try`)
   |     in this macro invocation
   |
   = help: the trait `std::ops::Try` is not implemented for `()`
   = note: required by `std::ops::Try::from_error`

error: aborting due to 3 previous errors

提前致谢。

4

0 回答 0