为什么从 Hyper crate 多次导入失败?

Why are multiple imports from the Hyper crate failing?

我正在用 Rust 开发一个非常简单的 HTTP 客户端,它建立在 hyper (GitHub, crates.io) crate 之上。

当我尝试使用 cargo build(以及使用 rustc src/main.rs)在新的 Cargo 项目中复制 examples/client.rs 文件时,由于从 hyper.

这是我的 main.rs 的顶部:

extern crate hyper;

use hyper::client::{Client, Request, Response, DefaultTransport as HttpStream};
use hyper::header::Connection;
use hyper::{Decoder, Encoder, Next};

除一些注释外,文件的其余部分与 hyper 存储库中的 examples/client.rs 文件相同。

在编译时,我得到以下错误:

src/main.rs:10:48: 10:78 error: unresolved import `hyper::client::DefaultTransport`. There is no `DefaultTransport` in `hyper::client` [E0432]
src/main.rs:10 use hyper::client::{Client, Request, Response, DefaultTransport as HttpStream};
                                                              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:10:48: 10:78 help: run `rustc --explain E0432` to see a detailed explanation
src/main.rs:12:13: 12:20 error: unresolved import `hyper::Decoder`. There is no `Decoder` in `hyper` [E0432]
src/main.rs:12 use hyper::{Decoder, Encoder, Next};
                           ^~~~~~~
src/main.rs:12:13: 12:20 help: run `rustc --explain E0432` to see a detailed explanation
src/main.rs:12:22: 12:29 error: unresolved import `hyper::Encoder`. There is no `Encoder` in `hyper` [E0432]
src/main.rs:12 use hyper::{Decoder, Encoder, Next};
                                    ^~~~~~~
src/main.rs:12:22: 12:29 help: run `rustc --explain E0432` to see a detailed explanation
src/main.rs:12:31: 12:35 error: unresolved import `hyper::Next`. There is no `Next` in `hyper` [E0432]
src/main.rs:12 use hyper::{Decoder, Encoder, Next};
                                             ^~~~
src/main.rs:12:31: 12:35 help: run `rustc --explain E0432` to see a detailed explanation
src/main.rs:53:6: 53:40 error: trait `hyper::client::Handler` is not in scope [E0405]
src/main.rs:53 impl hyper::client::Handler<HttpStream> for Dump {
                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/main.rs:53:6: 53:40 help: run `rustc --explain E0405` to see a detailed explanation
src/main.rs:53:6: 53:40 help: you can to import it into scope: `use hyper::server::Handler;`.

万一它可能有助于这个问题,这里是我的 Cargo.toml:

的内容
name = "my_project"
version = "0.0.1"
authors = ["email@example.com"]

[dependencies]
getopts = "0.2"
hyper = "0.9.6"

[[bin]]
name = "my_project"

有些导入确实有效,所以假设存储库中的示例是最新的,我真的不知道哪里出了问题。 crate 的源文件看起来像是公开了涉及的类型,但我对 Rust 很陌生,所以我可能会误读这些文件。

您正在使用 master 分支中的示例,这些示例不适用于 0.9.6 版本。您可以查看分支 0.9.6 上的示例,或者直接从 github 中使用 hyper,写在 Cargo.toml:

[dependencies]                                     
hyper = {git = "https://github.com/hyperium/hyper"}