1

我正在尝试使用此源作为教程使用 Iron 构建 Web 服务,但在编译 hyper 时出现错误:

Compiling hyper v0.9.17
/root/.cargo/registry/src/github.com-88ac128001ac3a9a/hyper-0.9.17/src/header/common/accept_language.rs:62:23: 62:30 error: macro undefined: 'langtag!'
/root/.cargo/registry/src/github.com-88ac128001ac3a9a/hyper-0.9.17/src/header/common/accept_language.rs:62                 qitem(langtag!(en;;;US)),
                                                                                                                                 ^~~~~~~
/root/.cargo/registry/src/github.com-88ac128001ac3a9a/hyper-0.9.17/src/header/common/accept_language.rs:63:34: 63:41 error: macro undefined: 'langtag!'
/root/.cargo/registry/src/github.com-88ac128001ac3a9a/hyper-0.9.17/src/header/common/accept_language.rs:63                 QualityItem::new(langtag!(en), Quality(500)),
                                                                                                                                            ^~~~~~~
/root/.cargo/registry/src/github.com-88ac128001ac3a9a/hyper-0.9.17/src/header/common/accept_language.rs:64:23: 64:30 error: macro undefined: 'langtag!'
/root/.cargo/registry/src/github.com-88ac128001ac3a9a/hyper-0.9.17/src/header/common/accept_language.rs:64                 qitem(langtag!(fr)),
                                                                                                                                 ^~~~~~~

我正在使用的版本:

  • 货物 0.8.0(建于 2016-03-22)
  • 锈病 1.7.0

货运.toml

[package]
name = "hello"
version = "0.1.0"
authors = ["root"]

[dependencies]
language-tags = "0.2.2"
iron = "0.4.0"

main.rs:

extern crate iron;

use iron::prelude::*;
use iron::status;
use iron::mime::Mime;

fn main() {
  Iron::new(|_: &mut Request| {
  let content_type = "application/json".parse::<Mime>().unwrap();

  Ok(Response::with((content_type, status::Ok, "{ respone: \"Hello world!\" }")))
 }).http("localhost:3009").unwrap();
}

我只在 Cargo.toml 中添加了语言标签,因为我认为它可以解决我的问题。没有进行其他更改。

4

1 回答 1

1

旧版本的 Rust 导致了这个问题。今天的稳定版 Rust 是 1.14.0,但在我的 Digital Ocean VM 上,预装了 Rust 1.7。即使运行官方安装程序后,版本仍然是 1.7:

curl https://sh.rustup.rs -sSf | sh

安装后它说:

欢迎来到锈!

这将下载并安装 Rust 编程语言的官方编译器及其包管理器 Cargo。

它会将 cargo、rustc、rustup 和其他命令添加到 Cargo 的 bin 目录,位于:

/root/.cargo/bin

然后,该路径将通过修改位于以下位置的配置文件添加到您的 PATH 环境变量中:

/root/.profile

我使用不使用 ~/.profile 的 zsh。所以PATH环境变量对我来说没有改变,因此命令cargo run指向预装的旧版本/usr/bin/cargo而不是~/.cargo/bin.

您可以使用命令which cargowhich rustc.

解决方案是使用~/.cargo/bin/cargo run. 对于 zsh,您还可以通过添加将文件夹添加~/.cargo/binPATH环境变量中

export PATH="~/.cargo/bin:$PATH"

到你的 .zshrc

于 2017-01-29T10:45:45.523 回答