How to Build a Web Application with Rocket in Rust

Build a Rust web app with Rocket by initializing a Cargo project, adding the dependency, defining a route, and running the server.

Initialize a new Cargo project, add the Rocket dependency, define a route handler, and launch the server.

cargo new my_app --bin
cd my_app
cargo add rocket

Create src/main.rs with the following content:

use rocket::get;

#[get("/")]
fn hello() -> &'static str {
    "Hello, world!"
}

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

Run the application:

cargo run --release

Visit http://127.0.0.1:8000/ in your browser.