微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决<'_> 未实现特征 `Serialize`

#[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。在呈现模板时将 vecIndexLink 添加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
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。