Learn ACTIX-WEB with Real Code Examples
Updated Nov 27, 2025
Code Sample Descriptions
1
Actix Web Simple REST API
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use serde::{Deserialize, Serialize};
use std::sync::Mutex;
#[derive(Serialize, Deserialize, Clone)]
struct Todo {
id: u32,
title: String,
completed: bool,
}
struct AppState {
todos: Mutex<Vec<Todo>>,
}
async fn get_todos(data: web::Data<AppState>) -> impl Responder {
let todos = data.todos.lock().unwrap();
HttpResponse::Ok().json(&*todos)
}
async fn add_todo(todo: web::Json<Todo>, data: web::Data<AppState>) -> impl Responder {
let mut todos = data.todos.lock().unwrap();
todos.push(todo.into_inner());
HttpResponse::Created().finish()
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let app_state = web::Data::new(AppState { todos: Mutex::new(Vec::new()) });
HttpServer::new(move || {
App::new()
.app_data(app_state.clone())
.route("/todos", web::get().to(get_todos))
.route("/todos", web::post().to(add_todo))
})
.bind("127.0.0.1:8080")?
.run()
.await
}
Demonstrates a simple Actix Web application with routes for listing and adding Todo items.