无法编译'http'包

时间:2015-05-13 02:34:21

标签: rust rust-cargo

我正在尝试Rust并尝试在this example之后执行简单的HTTP GET请求。首先,我运行cargo new request_learn --bin,然后将示例代码粘贴到main.rs,然后运行cargo build

request git:(master) ✗ cargo build
   Compiling request v0.0.1 (file:///Users/soliva/Sites/rust/app/request)
src/main.rs:12:23: 12:27 error: macro undefined: 'fail!'
src/main.rs:12         Err(error) => fail!(":-( {}", error),
                                 ^~~~
src/main.rs:17:35: 17:39 error: macro undefined: 'fail!'
src/main.rs:17         Err((_request, error)) => fail!(":-( {}", error),
                                             ^~~~
error: aborting due to 2 previous errors
Could not compile `request`.

我在这里更改了fail!宏,因为它似乎没有它。下一个版本说

request git:(master) ✗ cargo build
   Compiling request v0.0.1 (file:///Users/soliva/Sites/rust/app/request)
src/main.rs:1:1: 1:19 error: can't find crate for `http`
src/main.rs:1 extern crate http;
          ^~~~~~~~~~~~~~~~~~
error: aborting due to previous error
Could not compile `request`.

所以我添加到Cargo.toml

[dependencies]
http = "0.0.0"

我再次建造

request git:(master) ✗ cargo build --verbose
    Updating registry https://github.com/rust-lang/crates.io-index
   Compiling http v0.0.0-prealpha
   Running `rustc /Users/soliva/.cargo/registry/src/github.com-/.cargo/registry/src/github.com-1ecc6299db9ec823/http-0.0.0-prealpha/src/lib.rs:1:1: 1:20 error: The attribute license is currently unknown to the compiler and may have meaning added to it in the future
error: aborting due to previous error
Could not compile `http`.

Caused by:
  Process didn't exit successfully: `rustc /Users/soliva/.cargo/registry/src/github.com-1ecc6299db9ec823/http-0.0.0-prealpha/src/lib.rs --crate-name http --crate-type lib -g -C metadata=bec89420f16c6bf6 -C extra-filename=-bec89420f16c6bf6 --out-dir /Users/soliva/Sites/rust/app/request/target/debug/deps --emit=dep-info,link -L dependency=/Users/soliva/Sites/rust/app/request/target/debug/deps -L dependency=/Users/soliva/Sites/rust/app/request/target/debug/deps -Awarnings` (exit code: 101)

我剪切了一些错误消息,因为它很长且重复

我错过了什么吗?我正在研究Rust模块和cargo文档,但我似乎迷失了。

1 个答案:

答案 0 :(得分:1)

啊,是的,Rust-CI

问题是,Rust-CI是Travis甚至Cargo支持Rust之前的站点。在那些阴暗,黑暗的日子里,我们依靠Rust-CI来检测Rust nightlies的变化并自动更新Travis CI,以便我们的构建使用最新的Rust构建。它还充当了文档的存储库。

现在我们在个人Github Pages文档回购中使用Cargo和托管文档。

无论如何,正如其他人所说,rust-http crate已过时,建议替代方法是立即使用hyper crate。

[dependencies]
hyper = "0.5.0"

这是一个hyper usage example

extern crate hyper;

use std::io::Read;

use hyper::Client;
use hyper::header::Connection;

fn main() {
    // Create a client.
    let mut client = Client::new();

    // Creating an outgoing request.
    let mut res = client.get("http://www.gooogle.com/")
        // set a header
        .header(Connection::close())
        // let 'er go!
        .send().unwrap();

    // Read the Response.
    let mut body = String::new();
    res.read_to_string(&mut body).unwrap();

    println!("Response: {}", body);
}