gpt4 book ai didi

rust - 使用显式生命周期时,Rocket 的状态错误为 "Attempted to retrieve unmanaged state"?

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

当使用省略生命周期的 Rocket 的 State 时,可以正确处理对路由的请求:

#[post("/foo")]
pub fn foo_handler(db: State<Db>) {
// ...
}

但是,如果提供了明确的生命周期,那么 Rocket 对带有Attempted to retrieve unmanaged state! 的请求会出错:

#[post("/foo")]
pub fn foo_handler<'a>(db: State<&'a Db>) {
// ...
}

要么是编译器没有在此处拾取某些东西,要么是 Rocket 避免了安全检查,因为它编译正常,没有任何错误或警告。有什么想法吗?

最佳答案

这似乎是实现所需结果的方法:

#[post("/foo")]
pub fn foo_handler<'a>(db: State<'a, Db>) {
// ...
}

一个例子在 Rocket 的 State 中有所帮助文档。不过,我希望上述实现会抛出一个错误,因为它是有效的语法。

关于rust - 使用显式生命周期时,Rocket 的状态错误为 "Attempted to retrieve unmanaged state"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55884872/

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