1
0
mirror of https://github.com/actix/actix-website synced 2025-06-27 07:29:02 +02:00

fix note about app_data

This commit is contained in:
Rob Ede
2022-02-26 04:50:39 +00:00
parent d5533de730
commit 997fb14e79
3 changed files with 5 additions and 6 deletions

View File

@ -10,13 +10,14 @@ async fn index(data: web::Data<AppStateWithCounter>) -> String {
let mut counter = data.counter.lock().unwrap(); // <- get counter's MutexGuard
*counter += 1; // <- access counter inside MutexGuard
format!("Request number: {}", counter) // <- response with count
format!("Request number: {counter}") // <- response with count
}
// </setup_mutable>
// <make_app_mutable>
#[actix_web::main]
async fn main() -> std::io::Result<()> {
// Note: web::Data created _outside_ HttpServer::new closure
let counter = web::Data::new(AppStateWithCounter {
counter: Mutex::new(0),
});
@ -24,7 +25,6 @@ async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
// move counter into the closure
App::new()
// Note: using app_data instead of data
.app_data(counter.clone()) // <- register the created data
.route("/", web::get().to(index))
})

View File

@ -9,8 +9,7 @@ struct AppState {
#[get("/")]
async fn index(data: web::Data<AppState>) -> String {
let app_name = &data.app_name; // <- get app_name
format!("Hello {}!", app_name) // <- response with app_name
format!("Hello {app_name}!") // <- response with app_name
}
// </setup>