From 61a4b3b23b9ac64ad4ec18c848a605937bccff7c Mon Sep 17 00:00:00 2001 From: Douman Date: Sat, 27 Oct 2018 14:03:02 +0300 Subject: [PATCH] Improve shared state example --- state/src/main.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/state/src/main.rs b/state/src/main.rs index 2f0852d0..be1bd465 100644 --- a/state/src/main.rs +++ b/state/src/main.rs @@ -38,8 +38,10 @@ fn main() { env_logger::init(); let sys = actix::System::new("ws-example"); - server::new(|| { - App::with_state(AppState{counter: Arc::new(Mutex::new(0))}) // <- create app with state + let counter = Arc::new(Mutex::new(0)); + //move is necessary to give closure below ownership of counter + server::new(move || { + App::with_state(AppState{counter: counter.clone()}) // <- create app with shared state // enable logger .middleware(middleware::Logger::default()) // register simple handler, handle all methods