DEV Community

Cover image for Actix Web - The Rust Framework for Web Development - Hello World
Francesco Ciulla
Francesco Ciulla

Posted on

Actix Web - The Rust Framework for Web Development - Hello World

Hi! Francesco here.

Today, we will look at Actix, a high-performance framework for building web applications in Rust.

Actix Web site: https://actix.rs/

Whether you’re just starting with Rust or are an experienced developer exploring web capabilities, Actix offers a great way to dive into web development in Rust.

Known for its speed and efficiency, Actix consistently ranks among the fastest web frameworks, leveraging asynchronous execution by default to handle many requests concurrently. This makes it well-suited for applications requiring responsiveness under load.

Actix Hello World

A great plus for JavaScript Developers is that Actix looks similar to Express, so if you are familiar with Express, you will find Actix easy to learn.

In this article, we’ll build a simple "Hello World" web app with Actix, featuring three routes. This will give us a quick overview of the syntax and allow us to see if Actix might be a good fit for our projects.

If you prefer a Video version:


Setting Up the Project

First, let’s create a new Rust project and set up our dependencies.

  1. Create the Project:

    cargo new actix_hello_world
    
  2. Add Actix Dependency:
    Open Cargo.toml and add the actix-web dependency:

    [dependencies]
    actix-web = "4"
    

You project structure should look like this:

Actix Hello World

Writing the Code

Next, open main.rs and start building our application. Here’s the complete code:

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

#[get("/")]
async fn index() -> 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!")
}

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

Code Walkthrough

Let’s break down what each part of this code does:

  • We define our routes using Actix’s #[get("/")] and #[post("/echo")] attributes.
  • Each route maps to an asynchronous function.
  • The index function responds with "Hello world!" when accessed at the root (/) via a GET request (we will test it soon).
  • The echo function allows us to send a POST request to /echo. It returns the request body as the response, a simple echo server implementation.
  • The manual_hello function is another way to define a route. Instead of an attribute, we add it directly in the App setup using .route("/hey", web::get().to(manual_hello)).
  • The main function is where our server is created and configured.
  • Using HttpServer::new, we create an instance of App with our defined routes.
  • We then bind it to 127.0.0.1 on port 8080 and run it asynchronously.

Running and Testing the Application

Now, let’s test our Actix app (This might take a while the first time you run it):

cargo run
Enter fullscreen mode Exit fullscreen mode

Actix Hello World

Testing the Routes

Test GET Requests:

Open a browser and navigate to http://127.0.0.1:8080/, and you should see "Hello world!".

Actix Hello World

For the route /hey, go to http://127.0.0.1:8080/hey, and you’ll get "Hey there!".

Actix Hello World

Test POST Requests:

For the POST /echo route, use a tool like Postman to send a POST request to http://127.0.0.1:8080/echo with a body.

For example, if you send:

 {
  "test": 123
 }
Enter fullscreen mode Exit fullscreen mode

Actix Hello World

you’ll receive the same response body back (It work with a simple String, too).

Exploring More with Actix

What's my opinion about Actix? Actix’s syntax is concise, and it provides a great introduction to web programming with Rust. As we’ve seen, we can create an app with multiple routes in only a few lines of code.

Also, Actix’s documentation is extensive and easy to follow, which is always a positive sign for any project.

https://actix.rs/

The Actix GitHub repository has almost 30,000 stars, showing its popularity and active community.

Actix Hello World

I hope this was helpful, feel free to leave a comment if you have any questions or suggestions.

If you prefer a Video version:

Find me here: Francesco

Top comments (5)

Collapse
 
benborla profile image
Ben Borla

Love it! Would love to see more of this

Collapse
 
francescoxx profile image
Francesco Ciulla

you will. Have you checked the video too?

Collapse
 
benborla profile image
Ben Borla

Yes! I had an experience with Axum once, but ActiX seems to be an easy-to-use framework. I'll try using this on my next microservice. Great work @francescoxx

Thread Thread
 
francescoxx profile image
Francesco Ciulla

you are welcome. I will probably make another one about Axum too then I will start comparing them

Thread Thread
 
benborla profile image
Ben Borla

That would be awesome!