Getting Started with Actix Web

by Solomon Eseme

.

Updated Sat Dec 02 2023

Getting Started with Actix Web

This post is part of the "Actix Web: The Ultimate Guide (2023)" series

Before you build web applications with Actix Web, you'll need to set up a Rust development environment. If you need to, follow the official Rust installation instructions at https://www.rust-lang.org/learn/get-started.

Once Rust is installed, you can create a new Rust project and add Actix web as a dependency in your Cargo.toml file:

[dependencies]
actix-web = "4.4.0"

Now, let's create a simple Actix web application step by step.

Creating a Basic Actix Web Application

Create a new Rust project with the following command:

cargo new actix_web_demo
cd actix_web_demo

Next, open your project's Cargo.toml file and add Actix web as a dependency, as mentioned earlier. Then, your Cargo.toml should look like this:

[dependencies]
actix-web = "4.4.0"

Creating the Application Entry Point

In Rust, the entry point of your application is the main function. Create a main.rs file in your project's root directory and define the main function:

use actix_web::{get, App, HttpServer, Responder};

#[get("/")]
async fn hello() -> impl Responder {
    "Hello, Actix web!"
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new().service(hello)
    })
        .bind("127.0.0.1:8080")?
        .run()
        .await
}

In this code:

  • We import necessary items from Actix web.

  • We define a simple asynchronous function hello that responds with the string "Hello, Actix web!" when the root URL ("/") is accessed.

  • We create the main function which sets up an Actix web server. It uses HttpServer::new to configure the server and App::new to create an application with the hello route.

  • Finally, we bind the server to the address "127.0.0.1:8080" and run it asynchronously.

Running the Application

To run your Actix web application, use the following command from your project's root directory:

cargo run

This will start the Actix web server, and you'll see an output indicating that the server is running on 127.0.0.1:8080.

Accessing the Application

Open your web browser and navigate to http://localhost:8080. You should see the message "Hello, Actix web!" displayed in your browser. Alternatively, you can use the cURL tool to access the route from the terminal:

actix result

Congratulations! You've created a basic Actix web application.

Whenever you're ready

There are 4 ways we can help you become a great backend engineer:

The MB Platform

Join 1000+ backend engineers learning backend engineering. Build real-world backend projects, learn from expert-vetted courses and roadmaps, track your learnings and set schedules, and solve backend engineering tasks, exercises, and challenges.

The MB Academy

The “MB Academy” is a 6-month intensive Advanced Backend Engineering BootCamp to produce great backend engineers.

Join Backend Weekly

If you like post like this, you will absolutely enjoy our exclusive weekly newsletter, Sharing exclusive backend engineering resources to help you become a great Backend Engineer.

Get Backend Jobs

Find over 2,000+ Tailored International Remote Backend Jobs or Reach 50,000+ backend engineers on the #1 Backend Engineering Job Board

Read Full "Actix Web: The Ultimate Guide (2023)"

This is the most comprehensive tutorial on the Actix Web framework online.

In this Actix Web tutorial, you will learn Actix Web from scratch to an advanced level.

You will learn how to build and deploy your first Actix Web app.

Topic:

Backend Tips, Every week

Backend Tips, Every week