[Rust Rocket] Rocket - Getting Started
Rocket - Getting Started
Rocket is a web framework for Rust. If you’d like, you can think of Rocket as being a more flexible, friendly medley of Rails - https://rubyonrails.org/, Flask - https://palletsprojects.com/p/flask/, Bottle - https://bottlepy.org/docs/dev/index.html, and Yesod - https://www.yesodweb.com/. We prefer to think of Rocket as something new. Rocket aims to be fast, easy, and flexible while offering guaranteed safety and security where it can. Importantly, Rocket also aims to be fun, and it accomplishes this by ensuring that you write as little code as needed to accomplish your task.
Let’s create and run our first Rocket application. We’ll ensure we have a compatible Rust toolchain installed, create a new Cargo project that depends on Rocket, and then run the application.
Prerequisites
Installing Rust
Rocket makes use of the latest Rust features. Because of this, we’ll need a recent release of Rust to run Rocket applications. If you already have a working installation of the latest Rust compiler, feel free to skip to the next section.
1 | Install rustup by following the instructions |
See rustup.rs - The Rust toolchain installer - https://rustup.rs/ to learn more.
Note: You may prefer to develop using the nightly channel.
The nightly Rust toolchain enables certain improved developer experiences, such as better compile-time diagnostics, when developing with Rocket. You may choose to develop on the nightly channel to take advantage of these improved experiences. Note that all Rocket features are available across all Rust channels.
To set the nightly toolchain as your default, run rustup default nightly
.
Hello, world!
Let’s write our first Rocket application! Start by creating a new binary-based Cargo project and changing into the new directory:
1 | cargo new hello-rocket --bin |
Now, add Rocket as a dependency in your Cargo.toml
:
1 | # Cargo.toml |
1 | // src/main.rs |
It creates an index
route, mounts the route at the / path, and launches the application.
Run
Compile and run the program with cargo run
. You should see the following:
1 | cargo run |
Visit http://localhost:8000
to see your first Rocket application in action!
References
[1] Getting Started - Rocket Programming Guide - https://rocket.rs/v0.5-rc/guide/getting-started/
[2] Rocket - Simple, Fast, Type-Safe Web Framework for Rust - https://rocket.rs/
[3] SergioBenitez/Rocket: A web framework for Rust. - https://github.com/SergioBenitez/Rocket
[4] rustup.rs - The Rust toolchain installer - https://rustup.rs/