我有这个代码:
.and_then(move |key: Option<String>| async {
let pool = pool.clone();
let key = key.as_ref().map(|s| &**s);
match pool.get() {
Ok(conn) => Ok(Session::from_key(conn, key)),
Err(e) => {
error!("Failed to get a db connection");
Err(warp::reject::not_found())
}
}
})
.boxed()
我正在从这个例子中改编
但它给了我错误
lifetime may not live long enough
returning this value requires that `'1` must outlive `'2`
note: closure implements `Fn`, so references to captured variables can't escape the closurerustc
session.rs(131, 19): lifetime `'1` represents this closure's body
session.rs(131, 44): return type of closure is impl core::future::future::Future
session.rs(131, 46): returning this value requires that `'1` must outlive `'2`
async block may outlive the current function, but it borrows `key`, which is owned by the current function
may outlive borrowed value `key`rustc(E0373)
session.rs(131, 52): may outlive borrowed value `key`
session.rs(133, 23): `key` is borrowed here
我不得不将async
关键字添加到闭包以避免错误:
the trait bound `std::result::Result<session::Session, warp::reject::Rejection>: core::future::future::Future` is not satisfied
the trait `core::future::future::Future` is not implemented for `std::result::Result<session::Session, warp::reject::Rejection>`
note: required because of the requirements on the impl of `futures_core::future::TryFuture` for `std::result::Result<session::Session, warp::reject::Rejection>`rustc(E0277)
session.rs(138, 10): the trait `core::future::future::Future` is not implemented for `std::result::Result<session::Session, warp::reject::Rejection>`
所以,现在,看起来闭包正在返回一个特性,但是闭包被释放了,所以它会在使用之前尝试释放未来......关于如何解决这个问题的任何想法?