Skip to content

05. Web Development with Actix

Rust is increasingly popular for high-performance web development. Actix-web is a powerful, pragmatic, and extremely fast web framework for Rust.

Why Actix-web?

  • Type-Safe: Leverage Rustโ€™s type system for request/response handling.
  • Fast: Consistently ranks among the top web frameworks in performance benchmarks.
  • Extensible: Robust ecosystem of middleware and extensions.
  • Actor Model: Built on top of actix, though it can be used without direct actor interaction.

Core Concepts

1. The App

App is the entry point for your web application. It handles routing and state.

use actix_web::{web, App, HttpServer};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .route("/", web::get().to(|| async { "Hello World!" }))
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

2. Extractors

Extractors are used to get data from a request (query parameters, JSON bodies, paths).

use actix_web::{get, web, Responder};

#[get("/{id}/{name}/index.html")]
async fn index(path: web::Path<(u32, String)>) -> impl Responder {
    let (id, name) = path.into_inner();
    format!("Hello {}! id:{}", name, id)
}

3. Shared State

Use web::Data to share state across handlers (e.g., database connections, in-memory stores).


In the next section, we build a complete, production-ready CRUD implementation with Actix.