我想在处理程序函数中使用MySQL连接池:
l = [
(0, 7, 4),
(1, 7, 4),
(2, 7, 4),
(3, 7, 4),
(0, 4, 1),
(0, 4, 3),
(0, 6, 4)]
from itertools import groupby
from operator import itemgetter
l = [(x, (y, z)) for x, y, z in l]
l.sort(key=lambda x: x[0])
d = {key: list(list(zip(*tup))[1]) for key, tup in groupby(l, itemgetter(0))}
我收到错误
let pw = my::Pool::new("mysql://xxxxxx/dev");
let pool: my::Pool;
match pw {
Err(err) => {
panic!("{}", err);
}
Ok(p) => pool = p,
};
server::new(move || {
let pool = pool.to_owned();
App::new()
.middleware(Logger::default())
.middleware(Logger::new("%a %{User-Agent}i"))
.resource("/mytest", |r| {
r.method(http::Method::GET)
.f(|req: &HttpRequest| index(pool.clone(), req))
})
.resource("/mytest1", |r| {
r.method(http::Method::GET)
.f(|req: &HttpRequest| index(pool.clone(), req))
})
})
.bind("127.0.0.1:8088")
.unwrap()
.run();
编译器提出建议:
error[E0373]: closure may outlive the current function, but it borrows `pool`, which is owned by the current function
如果我在help: to force the closure to take ownership of `pool` (and any other referenced variables), use the `move` keyword
中使用move
,则不能在/mytest
中使用池。