DEV Community

Cover image for Rocket - The Rust Web Framework - Hello World
Francesco Ciulla
Francesco Ciulla

Posted on • Edited on

Rocket - The Rust Web Framework - Hello World

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 - The Rust Web Framework - Hello World

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 .


Enter fullscreen mode Exit fullscreen mode

Add the Rocket crate to your Cargo.toml file.



[dependencies]
rocket = "0.5.1"


Enter fullscreen mode Exit fullscreen mode

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])
}


Enter fullscreen mode Exit fullscreen mode

Now you can run the application using the following command.



cargo run


Enter fullscreen mode Exit fullscreen mode

And you should see the following output.

Rocket - The Rust Web Framework - Hello World

Now visit http://localhost:8000 in your web browser, and you should see the following message.

Rocket - The Rust Web Framework - Hello World

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.

Francesco

Top comments (6)

Collapse
 
chasm profile image
Charles F. Munat • Edited

How can you write an entire article about Rocket and not include an obvious link to the Rocket website?

It's rocket.rs/

Collapse
 
francescoxx profile image
Francesco Ciulla

added, thanks for letting me know.

Collapse
 
fyodorio profile image
Fyodor

FWIW, building a rocket with rust is a bad idea… not sure how this metaphor found its place anyways…

Collapse
 
francescoxx profile image
Francesco Ciulla

lol

Collapse
 
russoue profile image
Mohammad Husain

Still hasn’t reached 1.0, wow! How old is it again?

Collapse
 
francescoxx profile image
Francesco Ciulla

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