<'_> 未实现 `Serialize` 特征
the trait `Serialize` is not implemented for <'_>
#[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
。错误发生在呈现模板时将 IndexLink
的 vec
添加到 IndexContent
的行。我一定是在生命周期上做错了什么。
为什么会出现这个错误?
相关代码有多个语法错误。定义这些类型的有效方法如下:
#[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
}
#[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
。错误发生在呈现模板时将 IndexLink
的 vec
添加到 IndexContent
的行。我一定是在生命周期上做错了什么。
为什么会出现这个错误?
相关代码有多个语法错误。定义这些类型的有效方法如下:
#[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
}