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.
Building a web application with Rocket in Rust sets up a basic web server using the framework. You create a project folder, tell the system to download the Rocket tools, and write a small script that tells the server what to show when someone visits your website. It's like setting up a digital kiosk that displays a specific message to anyone who walks up to it.