我想要一个在所有处理程序中都可用的上下文结构,但我无法通过编译器。
例如,我想要这样的东西
extern crate iron;
extern crate router;
use iron::prelude::*;
use router::Router;
use std::collections::HashMap;
struct Context {
cache: HashMap<String, String>,
}
fn main() {
let mut context = Context { cache: HashMap::new(), };
let mut router = Router::new();
router.get("/", |request| index(request, context));
Iron::new(router).http("localhost:80").unwrap();
}
fn index(_: &mut Request, context: Context) -> IronResult<Response> {
Ok(Response::with((iron::status::Ok, "index")))
}
这不会编译一个冗长的错误消息
error: type mismatch resolving `for<'r, 'r, 'r> <[closure@src\main.rs:... context:_] as std::ops::FnOnce<(&'r mut iron::Request<'r, 'r>,)>>::Output == std::result::Result<iron::Response, iron::IronError>`:
expected bound lifetime parameter ,
found concrete lifetime [E0271]
src\main.rs:... router.get("/", |request| index(request, context));