gpt4 book ai didi

json - 即使类型具有派生(序列化),也不满足特征绑定(bind)序列化

转载 作者:行者123 更新时间:2023-11-29 08:00:33 25 4
gpt4 key购买 nike

我正在尝试使用 serde 宏序列化 JSON,但我只收到此错误:

src/models/pulse.rs:89:28: 89:49 error: the trait bound `models::pulse::Pulse: serde::ser::Serialize` is not satisfied [E0277]
src/models/pulse.rs:89 if let Ok(bulk_string) = serde_json::to_string(&self) {
^~~~~~~~~~~~~~~~~~~~~
src/models/pulse.rs:89:28: 89:49 help: run `rustc --explain E0277` to see a detailed explanation
src/models/pulse.rs:89:28: 89:49 note: required because of the requirements on the impl of `serde::ser::Serialize` for `&mut models::pulse::Pulse`
src/models/pulse.rs:89:28: 89:49 note: required by `serde_json::to_string`

我的代码好像没有什么问题。

#[derive(Serialize, Deserialize)]
pub struct Pulse {
#[serde(skip_serializing_if="Option::is_none")]
id: Option<u64>,

#[serde(skip_serializing_if="Option::is_none")]
category: Option<i64>,

#[serde(skip_serializing_if="Option::is_none")]
title: Option<String>,

}

是依赖错误吗?还是我的代码有问题?我用

[dependencies.serde]
git = "https://github.com/serde-rs/serde.git"
[dependencies.serde_macros]
git = "https://github.com/serde-rs/serde.git"
[dependencies.serde_json]
git = "https://github.com/serde-rs/json.git"

我有一个问题,为什么我使用 git 依赖项。原因是我得到:

/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-

0.18.0/src/mac.rs:6:5: 6:39 error: unresolved import `syntax::feature_gate::GatedCfgAttr`. There is no `GatedCfgAttr` in `syntax::feature_gate`. Did you mean to use `GatedCfg`? [E0432]
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:6 use syntax::feature_gate::GatedCfgAttr;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:6:5: 6:39 help: run `rustc --explain E0432` to see a detailed explanation
Compiling num-traits v0.1.32
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:113:18: 113:81 error: this function takes 4 parameters but 5 parameters were supplied [E0061]
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:113 let mut cx = ExtCtxt::new(sess, cfg, ecfg, feature_gated_cfgs, macro_loader);
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:113:18: 113:81 help: run `rustc --explain E0061` to see a detailed explanation
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:113:18: 113:81 note: the following parameter types were expected: &syntax::parse::ParseSess, std::vec::Vec<syntax::ptr::P<syntax::codemap::Spanned<syntax::ast::MetaItemKind>>>, syntax::ext::expand::ExpansionConfig<'_>, &mut syntax::ext::base::MacroLoader

我认为问题可能是您需要每晚使用。

最佳答案

rustc 1.20.0-nightlyserde 1.0.11serde-json 1.0.2 开始,这段代码编译时没有问题。

附带说明一下,您确实应该使用 crates.io 依赖项而不是 git 依赖项。

关于json - 即使类型具有派生(序列化),也不满足特征绑定(bind)序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37986118/

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