Getting Started

Installing Rust

If you don’t have Rust yet, we recommend you use rustup to manage your Rust installation. The official rust guide has a wonderful section on getting started.

Actix Web currently has a minimum supported Rust version (MSRV) of 1.42. Running rustup update will ensure you have the latest and greatest Rust version available. As such, this guide assumes you are running Rust 1.42 or later.

Hello, world!

Start by creating a new binary-based Cargo project and changing into the new directory:

cargo new hello-world
cd hello-world

Add actix-web as a dependency of your project by adding the following to your Cargo.toml file.

[dependencies]
actix-web = "3"

Request handlers use async functions that accept zero or more parameters. These parameters can be extracted from a request (see FromRequest trait) and returns a type that can be converted into an HttpResponse (see Responder trait):

use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};

#[get("/")]
async fn hello() -> impl Responder {
    HttpResponse::Ok().body("Hello world!")
}

#[post("/echo")]
async fn echo(req_body: String) -> impl Responder {
    HttpResponse::Ok().body(req_body)
}

async fn manual_hello() -> impl Responder {
    HttpResponse::Ok().body("Hey there!")
}

Notice that some of these handlers have routing information attached directly using the built-in macros. These allow you to specify the method and path that the handler should respond to. You will see below how to register the other route that does not use a routing macro.

Next, create an App instance and register the request handlers. Use App::service for the handlers using routing macros and App::route for manually routed handlers, declaring the path and method. Finally, the app is started inside an HttpServer which will serve incoming requests using your App as an “application factory”.

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

That’s it! Compile and run the program with cargo run. The #[actix_web::main] macro executes the async main function within the actix runtime. Now you can go to http://localhost:8080/ or any of the other routes you defined to see the results.

Next up: Application