[英]How to fix: expected concrete lifetime, but found bound lifetime parameter
I'm currently pulling my hear out over this one. 我目前正在就此发表自己的看法。 I tried to shrink it down to a minimal reproducible example.
我试图将其缩小到最小的可复制示例。
struct Request;
struct ResponseWriter<'a> {
dummy: &'a ()
}
#[deriving(Clone)]
pub struct RouteStore{
pub routes: Vec<Route>,
}
#[deriving(Clone)]
struct Route {
path: String,
handler: fn(request: &Request, response: &mut ResponseWriter)
}
impl RouteStore {
pub fn new () -> RouteStore {
RouteStore {
routes: Vec::new()
}
}
fn add_route (&mut self, path: String, handler: fn(request: &Request, response: &mut ResponseWriter)) -> () {
let route = Route {
path: path,
handler: handler
};
self.routes.push(route);
}
}
fn main () {
}
This leaves me with: 这给我留下了:
error: mismatched types: expected `fn(&http::server::request::Request, &mut http::server::response::ResponseWriter<>)` but found `fn(&http::server::request::Request, &mut http::server::response::ResponseWriter<>)` (expected concrete lifetime, but found bound lifetime parameter )
src/so.rs:12 handler: fn(request: &Request, response: &mut ResponseWriter)
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Previously I stored my fn
in a HashMap
like this HashMap<String, fn(request: &Request, response: &mut ResponseWriter)>
. 以前,我将
fn
像这样的HashMap<String, fn(request: &Request, response: &mut ResponseWriter)>
一样存储在HashMap
。 This worked fine. 这很好。
But now I want to refactor things a bit and introduced a Route
struct and store things as Vec<Route>
. 但是现在我想稍微重构一下,并引入了
Route
结构并将其存储为Vec<Route>
。 But suddenly hell breaks loose and I don't know how to fix it :-/ 但是突然间,地狱破裂了,我不知道该如何解决:-/
For the curious ones, this is part of my effort to write an expressjs inspired web framework for Rust called Floor 对于好奇的人,这是我为Rust写一个受expressjs启发的Web框架(称为Floor)的工作的一部分
After making your example a bit more minimal (getting rid of the http
dependency), and some suggestions on IRC (namely someone pointing out that the problem goes away if you remove the deriving(Clone)
from the Route
), you can see a fixed version below ( --cfg v2
) 在使示例变得更简单之后(摆脱了
http
依赖),并在IRC上提出了一些建议(即有人指出,如果从Route
删除deriving(Clone)
,问题将会消失),以下版本( --cfg v2
)
#[deriving(Clone)]
pub struct RouteStore{
pub routes: Vec<Route>,
}
#[cfg(v1)]
#[deriving(Clone)]
struct Route {
path: String,
handler: fn(response: &mut ())
}
#[cfg(v2)]
struct Route {
path: String,
handler: fn(response: &mut ())
}
#[cfg(v2)]
impl Clone for Route {
fn clone(&self) -> Route {
Route { path: self.path.clone(), handler: self.handler }
}
}
impl RouteStore {
pub fn new () -> RouteStore {
RouteStore {
routes: Vec::new()
}
}
fn add_route (&mut self, path: String, handler: fn(response: &mut ())) -> () {
let route = Route {
path: path,
handler: handler
};
self.routes.push(route);
}
}
fn main () {
}
The issue here is that an
fn
does not implement
Clone
.
这里的问题是
(edit: Well, it does have a fn
没有实现
Clone
。
clone
method, but the returned value does not seem compatible with what that field needs. The v2
version above just side-steps the whole issue.) (编辑:嗯,它确实有一个
clone
方法,但是返回的值似乎与该字段所需的值不兼容。上面的v2
版本只是绕开了整个问题。)
So I suspect the error you are seeing is coming from the auto-generated clone implementation that deriving(Clone)
injects. 因此,我怀疑您看到的错误是来自
deriving(Clone)
注入的自动生成的克隆实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.