gpt4 book ai didi

rust - 带有 serde 的项目无法编译

转载 作者:行者123 更新时间:2023-11-29 08:21:22 35 4
gpt4 key购买 nike

当我尝试运行示例时 from the serde repository :

#![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);
}

我得到一个错误:

error: failed to run rustc to learn about target-specific information

Caused by: process didn't exit successfully: rustc - --crate-name _
--print=file-names --crate-type bin --crate-type proc-macro --crate-type rlib --target x86_64-unknown-linux-gnu
(exit code: 101) --- stderr error: unknown crate type: proc-macro

我的 Rust 版本是 1.13.0,我的 Cargo.toml 有这些依赖项:

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

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

最佳答案

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

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

关于rust - 带有 serde 的项目无法编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41167080/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com