Skip to content

Instantly share code, notes, and snippets.

@dtoebe
Created September 16, 2018 23:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dtoebe/0c4f97684d072baa6a84d405bb6cedc6 to your computer and use it in GitHub Desktop.
Save dtoebe/0c4f97684d072baa6a84d405bb6cedc6 to your computer and use it in GitHub Desktop.
cargo build [16:54:54]
Compiling restapi v0.1.0 (file:restapi)
error[E0507]: cannot move out of captured variable in an `Fn` closure
--> src/main.rs:57:62
|
55 | let path = String::from("/path/to/data.json");
| ---- captured outer variable
56 | server::new(|| {
57 | App::with_state(AppState::new(AppState::collect_data(path).unwrap()))
| ^^^^ cannot move out of captured variable in an `Fn` closure
error[E0507]: cannot move out of `path`, as it is a captured variable in a `Fn` closure
--> src/main.rs:57:62
|
57 | App::with_state(AppState::new(AppState::collect_data(path).unwrap()))
| ^^^^
| |
| cannot move out of `path`, as it is a captured variable in a `Fn` closure
| cannot move
|
help: consider changing this to accept closures that implement `FnMut`
--> src/main.rs:56:17
|
56 | server::new(|| {
| _________________^
57 | | App::with_state(AppState::new(AppState::collect_data(path).unwrap()))
58 | | .resource("/", |r| r.f(get_all_comments))
59 | | }).bind(format!("{}:{}", HOST, PORT))
| |_____^
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0507`.
error: Could not compile `restapi`.
To learn more, run the command again with --verbose.
extern crate actix;
extern crate actix_web;
extern crate rand;
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
use actix_web::{server, App, HttpRequest, HttpResponse};
use std::collections::hash_map::HashMap;
use std::error::Error;
use std::fs::File;
const HOST: &str = "127.0.0.1";
const PORT: &str = "3030";
#[derive(Deserialize, Serialize, Debug, Clone)]
struct Comment {
id: u32,
name: String,
email: String,
body: String,
}
#[derive(Deserialize, Serialize, Debug)]
struct AppState {
data: HashMap<u32, Comment>,
}
impl AppState {
fn new(comments: Vec<Comment>) -> AppState {
let mut data = HashMap::new();
for com in comments {
data.insert(com.id, com.clone());
}
AppState { data: data }
}
fn collect_data(path: String) -> Result<Vec<Comment>, Box<Error>> {
let file = File::open(&path)?;
let raw_data: HashMap<String, Vec<Comment>> = serde_json::from_reader(&file)?;
let data: Vec<Comment> = raw_data.get("data").unwrap().to_vec();
Ok(data)
}
}
fn get_all_comments(req: &HttpRequest<AppState>) -> HttpResponse {
println!("{:?}", req);
HttpResponse::Ok().body("")
}
fn main() {
let sys = actix::System::new("Copperhead Challenge");
let path = String::from("/path/to/data.json");
server::new(|| {
// Error Here
App::with_state(AppState::new(AppState::collect_data(path).unwrap()))
.resource("/", |r| r.f(get_all_comments))
}).bind(format!("{}:{}", HOST, PORT))
.unwrap()
.start();
println!("Listening on: {}:{}", HOST, PORT);
let _ = sys.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment