Rocket - The Rust Web Framework - Hello World
In this article, we will learn how to build a simple web application using Rocket, a web framework for Rust.
If you prefer a video version
Rocket
Rocket is a web framework for Rust that makes writing fast, secure web applications simple without sacrificing flexibility, usability, or type safety.
Rocket is type-safe, which means that many kinds of errors are caught at compile time. Rocket is also fast, with a focus on performance and speed.
Rocket is secure, with built-in support for common security vulnerabilities. Rocket is flexible, with a powerful routing system that allows you to define routes using a simple, intuitive syntax.
Rocket is also easy to use, with a clean, well-documented API that makes it easy to start.
Let's look at how to get started with Rocket and build a simple web application.
Getting Started
Create a new Rust project using Cargo, enter the project directory, and open the project in your favorite code editor.
cargo new hello-rocket
cd hello-rocket
code .
Add the Rocket crate to your Cargo.toml
file.
[dependencies]
rocket = "0.5.1"
Then, replace the content of src/main.rs
with the following code.
#[macro_use] extern crate rocket;
#[get("/")]
fn index() -> &'static str {
"Hello, Rocket 🚀"
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![index])
}
Now you can run the application using the following command.
cargo run
And you should see the following output.
Now visit http://localhost:8000
in your web browser, and you should see the following message.
That's it! You've just built a simple web application using Rocket.
If you prefer a video version
Conclusion
Rocket is a powerful web framework for Rust that makes writing fast, secure web applications simple.
In this article, we've covered the basics of Rocket and how to get started with building a simple web application.
I hope you found this article helpful! If you have any questions or comments, please leave them below.
Have fun coding with Rust.
Top comments (6)
How can you write an entire article about Rocket and not include an obvious link to the Rocket website?
It's rocket.rs/
added, thanks for letting me know.
FWIW, building a rocket with rust is a bad idea… not sure how this metaphor found its place anyways…
lol
Still hasn’t reached 1.0, wow! How old is it again?
I think the first version has been released in 2016. It has very high standads, so it might still take a while and that's fine