[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
2
3
4
5
# Install rustup by following the instructions
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

# Ensure the latest toolchain is installled by running the command
$ rustup default stable

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
2
3
$ cargo new hello-rocket --bin

cd hello-rocket

Now, add Rocket as a dependency in your Cargo.toml:

1
2
3
4
# Cargo.toml

[dependencies]
rocket = "0.5.0-rc.1"
1
2
3
4
5
6
7
8
9
10
11
12
13
// src/main.rs

#[macro_use] extern crate rocket;

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

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

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
2
3
4
5
6
7
8
9
10
11
12
13
14
$ cargo run
🔧 Configured for debug.
>> address: 127.0.0.1
>> port: 8000
>> workers: [..]
>> keep-alive: 5s
>> limits: [..]
>> tls: disabled
>> temp dir: /tmp
>> log level: normal
>> cli colors: true
🛰 Routes:
>> (index) GET /
🚀 Rocket has launched from http://127.0.0.1:8000

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/

[5] Rust Programming Language - https://www.rust-lang.org/