2

当我尝试从 serde 存储库运行示例时:

#![feature(proc_macro)]

#[macro_use]
extern crate serde_derive;

extern crate serde_json;

#[derive(Serialize, Deserialize, Debug)]
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let point = Point { x: 1, y: 2 };

    // Convert the Point to a JSON string.
    let serialized = serde_json::to_string(&point).unwrap();

    // Prints serialized = {"x":1,"y":2}
    println!("serialized = {}", serialized);

    // Convert the JSON string back to a Point.
    let deserialized: Point = serde_json::from_str(&serialized).unwrap();

    // Prints deserialized = Point { x: 1, y: 2 }
    println!("deserialized = {:?}", deserialized);
}

我收到一个错误:

错误:未能运行rustc以了解特定于目标的信息

原因:进程没有成功退出:(rustc - --crate-name _ --print=file-names --crate-type bin --crate-type proc-macro --crate-type rlib --target x86_64-unknown-linux-gnu退出代码:101)---stderr错误:未知的箱子类型:proc-macro

我的 Rust 版本是 1.13.0 并且我的 Cargo.toml 具有以下依赖项:

[dependencies]
serde = "*"
serde_derive = "*"

我应该使用其他依赖项还是额外的配置?

4

1 回答 1

0

#![feature(...)]属性表示使用尚未稳定的 Rust 特性的代码。在提出问题时,该proc_macro功能还不稳定。Serde 的#[derive(Serialize, Deserialize)]宏需要此功能。

自Rust 1.15起,自定义派生已稳定,因此问题中的代码(已删除特性属性)应该适用于该版本以来的任何 Rust 编译器。

于 2018-02-25T20:50:05.480 回答