I'm facing this error again and again and again and still can't build proper mindset to write code without this error
so I have simple function (axum handler)
async fn ip(State(AppState { db, cache }): State<AppState>, Path(ip): Path<Ipv4Addr>) -> String {
let future = cache
.lock()
.unwrap()
.entry(ip.to_bits())
.or_insert_with(move || db.get_country(ip).boxed().shared())
.clone();
let res = future.await;
res
}
and the error is
error[E0515]: cannot return value referencing local data `db`
--> src/main.rs:86:29
|
86 | .or_insert_with(move || db.get_country(ip).boxed().shared())
| --^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| returns a value referencing data owned by the current function
| `db` is borrowed here