简体   繁体   English

如何解决:预期的混凝土寿命,但找到了约束寿命参数

[英]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 . 这里的问题是 fn没有实现 Clone (edit: Well, it does have a 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.

相关问题 预期的约束寿命参数,找到具体的寿命 - Expected bound lifetime parameter, found concrete lifetime 预期的约束寿命参数,找到的混凝土寿命[E0271] - Expected bound lifetime parameter, found concrete lifetime [E0271] StreamExt.scan() 方法上的“预期绑定生命周期参数,找到具体生命周期” - “expected bound lifetime parameter, found concrete lifetime” on StreamExt .scan() method 锈蚀寿命误差预期具体寿命但发现约束寿命 - Rust lifetime error expected concrete lifetime but found bound lifetime 函数引用:预期的约束生命周期参数,找到具体的生命周期[E0271] - Function references: expected bound lifetime parameter , found concrete lifetime [E0271] 预期绑定生命周期参数,在尝试传递选项时找到具体生命周期<fnonce></fnonce> - Expected bound lifetime parameter, found concrete lifetime when trying to pass an Option<FnOnce> 预期的混凝土寿命,在结构中存储fn时找到约束寿命参数 - Expected concrete lifetime, found bound lifetime parameter when storing a fn in a struct 当使用参数的生命周期作为特征参数时,“预期的关联类型,找到`u32`” - “Expected associated type, found `u32`” when using the lifetime of a parameter as trait parameter in where bound 从闭包填充集合时,键入不匹配“绑定生命周期参数”与“具体生命周期” - Type mismatch “bound lifetime parameter” vs “concrete lifetime” when filling a collection from a closure 为什么此特性/实现不兼容-约束寿命与具体寿命 - Why is this trait/implementation incompatible - bound lifetime vs concrete lifetime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM