任何在Rust中使用Hyper的建议

时间:2017-11-03 16:52:05

标签: rust hyper

我一直在寻找任何有关生锈的高箱子的工作示例和良好的文档,并且遇到了一个示例here。 但是,当我运行该示例时,我会遇到几个错误。

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

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

看起来这个示例已经过时了,所以从超级文档中获取示例并添加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

提前致谢。

0 个答案:

没有答案
相关问题