2

我正在尝试 Rust 并尝试按照这个示例执行一个简单的 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文档,但我似乎迷路了。

4

1 回答 1

1

啊,是的,Rust-CI

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

现在我们使用Cargo并在个人 Github Pages 文档 repo 上托管 doc。


无论如何,正如其他人所指出的那样,rust-httpcrate 已经过时,建议的替代方法是现在使用hypercrate。

[dependencies]
hyper = "0.5.0"

这是一个超级用法示例

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);
}
于 2015-05-13T10:29:35.350 回答