gpt4 book ai didi

rust - 为什么我要获得 “Cannot Derive Macro In This Scope”?

转载 作者:行者123 更新时间:2023-12-03 11:42:01 26 4
gpt4 key购买 nike

尝试针对此代码cargo build:

#![allow(unused)]

use serde::{Deserialize, Serialize};
use serde_json::{Result, Value};

#[derive(Serialize, Deserialize,Debug)]
struct Repository{
r#type: String,
url: String,
}

fn main() {
println!("Hello, world!");
}

这是cargo.toml文件:
[package]
name = "demo_err"
version = "0.1.0"
authors = ["Onorio Catenacci <catenacci@ieee.org>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = "1.0.104"
serde_json = "1.0.44"

当然,我的真实代码要大一些,但这是我可以重现错误的最小代码。

我收到以下错误:
   Compiling demo_err v0.1.0 (U:\skunkworks\rust\demo_err)
error: cannot find derive macro `Serialize` in this scope
--> src\main.rs:9:10
|
9 | #[derive(Serialize, Deserialize,Debug)]
| ^^^^^^^^^

error: cannot find derive macro `Deserialize` in this scope
--> src\main.rs:9:21
|
9 | #[derive(Serialize, Deserialize,Debug)]
| ^^^^^^^^^^^

现在我假设我做错了-除了来自serde_json的 sample code。就像这样:
use serde::{Deserialize, Serialize};
use serde_json::Result;

#[derive(Serialize, Deserialize)]
struct Person {
name: String,
age: u8,
phones: Vec<String>,
}

现在,一个明显的区别是 r#type,但是使用其他名称不能解决该错误。另一个明显的区别是 serde_json::{Result, Value},但是删除 Value也不能解决该错误。

显然,我的代码和该示例之间有些不同,但是对于我的一生,我无法弄清楚有什么不同。有人能指出我正确的方向吗?

最佳答案

您需要激活所需的功能才能使用派生宏。您可以通过更改serde中的cargo.toml声明来做到这一点:

serde = { version = "1.0.104", features = ["derive"] }

有关更多信息,请关注: https://serde.rs/derive.html

另请参见:
  • How do I fix "cannot find derive macro in this scope"?
  • 关于rust - 为什么我要获得 “Cannot Derive Macro In This Scope”?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60676916/

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