Create a basic web server in Rust with Rocket

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Learn how to create a basic web server in Rust with the Rocket framework. A practical guide to installing, configuring, and launching your first HTTP routes in a simple and fast way.

Article published on 02/09/2025, last updated on 10/08/2026

Rust is renowned for its safety, performance, and reliability. But did you know that it's also possible to build web applications in Rust?

Among the available frameworks, Rocket is one of the newest and simplest, as it lets you quickly get a web server up and running in just a few lines!

Creating a project with Cargo

First of all, make sure you have Rust and Cargo installed on your machine.

If that's not the case, follow our article on getting started with Rust: https://code-garage.com/blog/debuter-avec-rust-installer-et-executer-un-programme

Then, all you need to do is create a new project with the following command:

cargo new basic-web-server
cd basic-web-server

Next, add Rocket and Serde (for data serialization) to your Cargo.toml file:

[dependencies]
rocket = "0.5.0"
serde = { version = "1.0", features = ["derive"] }

We are now ready to create our server!

A first minimalist server

Creating a web server is disarmingly simple, here's the smallest possible example:

#[macro_use] extern crate rocket;

#[get("/")]
fn index() -> String {
    String::from("Web server is running!")
}

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![index])
}

Here:

  • We define a "/" route accessible via HTTP GET.
  • The index function returns a simple string.
  • The server is launched with rocket::build() and #[launch].

All you need to do is run the server with cargo run, and that's it!

Result: if you open http://localhost:8000, you'll see "Web server is running!" appear.

Passing parameters to a route

Rocket really becomes interesting when you want to customize responses. For instance, you can create a route that greets the user based on their name:

#[macro_use] extern crate rocket;

#[get("/hello/<name>")]
fn hello_name(name: &str) -> String {
    format!("Hello, {}!", name)
}

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![hello_name])
}

With this code, if you type http://localhost:8000/hello/Alice in your browser, the server will reply: "Hello, Alice!"

Rocket automatically takes care of extracting the <name> parameter from the URL and passing it to your function.

Returning JSON

In the real world, APIs largely return the JSON format and Rocket natively supports this thanks to the serde module.

Here's a simple example that returns a JSON response:

#[macro_use] extern crate rocket;
use rocket::serde::{Serialize, json::Json};

#[derive(Serialize)]
struct Message<'r> {
    text: &'r str,
}

#[get("/json")]
fn hello_json() -> Json<Message<'static>> {
    Json(Message { text: "Hello, JSON!" })
}

#[launch]
fn rocket() -> _ {
    rocket::build().mount("/", routes![hello_json])
}

This time, accessing http://localhost:8000/json will return:

{
  "text": "Hello, JSON!"
}

In summary

Rocket is ideal for discovering web development in Rust, with just a few lines of code, you can:

  • start a server,
  • create dynamic routes,
  • serve JSON.

Whether you want to learn Rust or build high-performance web applications, Rocket shows you it's possible.

Find the official Rocket documentation right here: https://rocket.rs/


Finished reading this article?
Our newsletter

No spam. Only free content, news, and ever more resources to level up your skills!

Join +1500 developers

Comments (0)

to leave a comment

No comments yet