gpt4 book ai didi

rust - <'_> 未实现特征 `Serialize`

转载 作者:行者123 更新时间:2023-12-04 07:44:46 25 4
gpt4 key购买 nike

#[derive(serde::Serialize)]
struct IndexLink<'r>{
text: &'r str,
link: &'r str;
}

#[derive(serde::Serialize)]
struct IndexContext<'r> {
title: &'r str,
links: Vec<&'r IndexLink<&'r>>
}

#[get("/")]
pub fn index() -> Template {
Template::render("index", &IndexContext{
title : "My home on the web",
links: vec![IndexLink{text: "About", link: "/about"}, IndexLink{text: "RSS Feed", link: "/feed"}]
})
}

原因 error[E0277]: the trait bound IndexContext<'_>: Serialize is not satisfied .错误发生在添加 vec 的行中的 IndexLinkIndexContent渲染模板时。我一定是在生命周期上做错了什么。

为什么会出现这个错误?

最佳答案

有问题的代码有多个语法错误。定义这些类型的有效方法如下:

#[derive(serde::Serialize)]
struct IndexLink<'r>{
text: &'r str,
link: &'r str, // no semicolon inside struct definition
}

#[derive(serde::Serialize)]
struct IndexContext<'r> {
title: &'r str,
links: Vec<&'r IndexLink<'r>>, // lifetime parameter is just 'r, not &'r
}

关于rust - <'_> 未实现特征 `Serialize`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67250246/

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