Advertisement
Guest User

Untitled

a guest
Nov 1st, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.57 KB | None | 0 0
  1. use actix_web::http;
  2. use rusqlite::Connection;
  3. use std::sync::Arc;
  4. use std::sync::Mutex;
  5.  
  6. mod api;
  7. mod db;
  8.  
  9. fn main() {
  10.     println!("Hello, world!");
  11.     let db_connection = db::load_database("data.sqlite");
  12.     start_server(db_connection);
  13. }
  14.  
  15. fn start_server(db_connection: Connection) {
  16.     let safe_db_connection = Arc::new(Mutex::new(db_connection));
  17.     server::new( || App::new()
  18.         .handler("/api/online", api::online)
  19.         .route("/api/card/{id}", http::Method::GET, |req| api::card(req,safe_db_connection.clone())))
  20.         .bind("127.0.0.1:8080").unwrap()
  21.         .run();
  22. }
  23. // Definitions from other files
  24. pub fn load_database(file_name: &str) -> Connection {...}
  25. pub fn api::card(request: Path<i32>, db_connection: Arc<Mutex<Connection>>) -> impl Responder {...}
  26.  
  27. // Exact error
  28. /*
  29. error[E0373]: closure may outlive the current function, but it borrows `safe_db_connection`, which is owned by the current function
  30.   --> src/main.rs:25:18
  31.    |
  32. 25 |     server::new( || App::new()
  33.    |                  ^^ may outlive borrowed value `safe_db_connection`
  34. 26 |         .handler("/api/online", api::online)
  35. 27 |         .route("/api/card/{id}", http::Method::GET, |req| api::card(req,safe_db_connection.clone())))
  36.    |                                                                         ------------------ `safe_db_connection` is borrowed here
  37. help: to force the closure to take ownership of `safe_db_connection` (and any other referenced variables), use the `move` keyword
  38.    |
  39. 25 |     server::new( move || App::new()
  40.    |                  ^^^^^^^
  41. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement