1

私は、さびのハイパークレートの実用的な例と優れたドキュメントを探していて、ここに例を見つけました。ただし、その例を実行すると、いくつかのエラーが発生します。

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

前もって感謝します。

4

0 に答える 0