具有生命周期值的 Rust 函数 return 类型
Rust function return type with lifetime value
我想为我的 Hero
结构实现 Responder
特性,但是下面的方法签名 (respond_to
):
use rocket::{http::Status, response::Responder, Request, Response};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Hero {
pub id: Option<i32>,
pub name: String,
pub identity: String,
pub hometown: String,
pub age: i32,
}
impl Hero {
pub fn new(
num: Option<i32>,
name: String,
identity: String,
hometown: String,
age: i32,
) -> Hero {
Hero {
id: num,
name,
identity,
hometown,
age,
}
}
}
impl<'r> Responder<'r> for Hero {
fn respond_to(self, _request: &Request) -> Result<Response, Status> {
unimplemented!()
}
}
引发编译错误:
error[E0106]: missing lifetime specifier
--> src/main.rs:32:55
|
32 | fn respond_to(self, _request: &Request) -> Result<Response, Status> {
| ^^^^^^^^ expected lifetime parameter
|
= help: this function's return type contains a borrowed value, but the signature does not say which one of `_request`'s 2 lifetimes it is borrowed from
依赖关系:
[dependencies]
rocket = "0.4.2"
serde = {version = "1.0.99", features=["derive"]}
该文档未提供有关如何在返回类型时提供生命周期的示例。返回类型时如何指定生命周期?
Responder
特征定义为:
pub trait Responder<'r> {
fn respond_to(self, request: &Request) -> response::Result<'r>;
}
response::Result<'r>
定义为:
pub type Result<'r> = ::std::result::Result<self::Response<'r>, ::http::Status>;
您的方法签名是:
fn respond_to(self, request: &Request) -> Result<Response, Status>;
如您所见,您只是忘记为 Response
指定生命周期。正确的方法签名是:
impl<'r> Responder<'r> for Hero {
fn respond_to(self, request: &Request) -> Result<Response<'r>, Status> {
unimplemented!()
}
}
我想为我的 Hero
结构实现 Responder
特性,但是下面的方法签名 (respond_to
):
use rocket::{http::Status, response::Responder, Request, Response};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Hero {
pub id: Option<i32>,
pub name: String,
pub identity: String,
pub hometown: String,
pub age: i32,
}
impl Hero {
pub fn new(
num: Option<i32>,
name: String,
identity: String,
hometown: String,
age: i32,
) -> Hero {
Hero {
id: num,
name,
identity,
hometown,
age,
}
}
}
impl<'r> Responder<'r> for Hero {
fn respond_to(self, _request: &Request) -> Result<Response, Status> {
unimplemented!()
}
}
引发编译错误:
error[E0106]: missing lifetime specifier
--> src/main.rs:32:55
|
32 | fn respond_to(self, _request: &Request) -> Result<Response, Status> {
| ^^^^^^^^ expected lifetime parameter
|
= help: this function's return type contains a borrowed value, but the signature does not say which one of `_request`'s 2 lifetimes it is borrowed from
依赖关系:
[dependencies]
rocket = "0.4.2"
serde = {version = "1.0.99", features=["derive"]}
该文档未提供有关如何在返回类型时提供生命周期的示例。返回类型时如何指定生命周期?
Responder
特征定义为:
pub trait Responder<'r> {
fn respond_to(self, request: &Request) -> response::Result<'r>;
}
response::Result<'r>
定义为:
pub type Result<'r> = ::std::result::Result<self::Response<'r>, ::http::Status>;
您的方法签名是:
fn respond_to(self, request: &Request) -> Result<Response, Status>;
如您所见,您只是忘记为 Response
指定生命周期。正确的方法签名是:
impl<'r> Responder<'r> for Hero {
fn respond_to(self, request: &Request) -> Result<Response<'r>, Status> {
unimplemented!()
}
}