2017-04-17 02:48:59 +00:00
|
|
|
# Getting Started
|
|
|
|
|
|
|
|
Let's create and run our first Rocket application. We'll ensure we have a
|
2020-07-21 21:28:49 +00:00
|
|
|
compatible Rust toolchain installed, create a new Cargo project that depends on
|
|
|
|
Rocket, and then run the application.
|
2017-04-17 02:48:59 +00:00
|
|
|
|
|
|
|
## Installing Rust
|
|
|
|
|
2020-07-21 21:28:49 +00:00
|
|
|
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.
|
2017-04-17 02:48:59 +00:00
|
|
|
|
2021-02-19 06:04:42 +00:00
|
|
|
To install the latest version of Rust, we recommend using `rustup`. Install
|
2018-01-13 04:06:27 +00:00
|
|
|
`rustup` by following the instructions on [its website](https://rustup.rs/).
|
2022-07-16 06:21:14 +00:00
|
|
|
Once `rustup` is installed, ensure the latest toolchain is installed by running
|
2020-07-21 21:28:49 +00:00
|
|
|
the command:
|
2017-04-17 02:48:59 +00:00
|
|
|
|
|
|
|
```sh
|
2020-07-21 21:28:49 +00:00
|
|
|
rustup default stable
|
2017-04-17 02:48:59 +00:00
|
|
|
```
|
|
|
|
|
2020-07-21 21:28:49 +00:00
|
|
|
! note: You may prefer to develop using the _nightly_ channel.
|
2017-04-17 02:48:59 +00:00
|
|
|
|
2020-07-21 21:28:49 +00:00
|
|
|
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.
|
2017-04-17 02:48:59 +00:00
|
|
|
|
2020-07-21 21:28:49 +00:00
|
|
|
To set the nightly toolchain as your default, run `rustup default nightly`.
|
2017-04-17 02:48:59 +00:00
|
|
|
|
|
|
|
## Hello, world!
|
|
|
|
|
|
|
|
Let's write our first Rocket application! Start by creating a new binary-based
|
|
|
|
Cargo project and changing into the new directory:
|
|
|
|
|
|
|
|
```sh
|
|
|
|
cargo new hello-rocket --bin
|
|
|
|
cd hello-rocket
|
|
|
|
```
|
|
|
|
|
2018-10-22 21:47:35 +00:00
|
|
|
Now, add Rocket as a dependency in your `Cargo.toml`:
|
2017-04-17 02:48:59 +00:00
|
|
|
|
2020-02-15 11:43:47 +00:00
|
|
|
```toml
|
2017-04-17 02:48:59 +00:00
|
|
|
[dependencies]
|
2023-11-02 00:18:11 +00:00
|
|
|
rocket = "=0.5.0-rc.4"
|
2017-04-17 02:48:59 +00:00
|
|
|
```
|
|
|
|
|
2021-03-27 21:20:43 +00:00
|
|
|
! warning: Development versions must be _git_ dependencies.
|
|
|
|
|
|
|
|
Development versions, tagged with `-dev`, are not published. To depend on a
|
|
|
|
development version of Rocket, you'll need to point `Cargo.toml` to a Rocket
|
|
|
|
git repository. For example, with `######` replaced with a git commit hash:
|
|
|
|
|
|
|
|
`
|
|
|
|
[dependencies]
|
|
|
|
`
|
|
|
|
`
|
|
|
|
rocket = { git = "https://github.com/SergioBenitez/Rocket", rev = "######" }
|
|
|
|
`
|
|
|
|
|
2017-04-17 02:48:59 +00:00
|
|
|
Modify `src/main.rs` so that it contains the code for the Rocket `Hello, world!`
|
2017-07-03 05:51:24 +00:00
|
|
|
program, reproduced below:
|
2017-04-17 02:48:59 +00:00
|
|
|
|
|
|
|
```rust
|
2018-10-05 04:44:42 +00:00
|
|
|
#[macro_use] extern crate rocket;
|
2017-04-17 02:48:59 +00:00
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
fn index() -> &'static str {
|
|
|
|
"Hello, world!"
|
|
|
|
}
|
|
|
|
|
2020-07-21 21:28:49 +00:00
|
|
|
#[launch]
|
2021-04-14 01:12:39 +00:00
|
|
|
fn rocket() -> _ {
|
2021-04-08 08:07:52 +00:00
|
|
|
rocket::build().mount("/", routes![index])
|
2017-04-17 02:48:59 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
We won't explain exactly what the program does now; we leave that for the rest
|
|
|
|
of the guide. In short, it creates an `index` route, _mounts_ the route at the
|
2017-07-03 05:51:24 +00:00
|
|
|
`/` path, and launches the application. Compile and run the program with `cargo
|
|
|
|
run`. You should see the following:
|
2017-04-17 02:48:59 +00:00
|
|
|
|
|
|
|
```sh
|
2020-11-03 20:56:20 +00:00
|
|
|
> cargo run
|
|
|
|
🔧 Configured for debug.
|
2021-04-14 04:59:11 +00:00
|
|
|
>> 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 /
|
2020-11-03 20:56:20 +00:00
|
|
|
🚀 Rocket has launched from http://127.0.0.1:8000
|
2017-04-17 02:48:59 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Visit `http://localhost:8000` to see your first Rocket application in action!
|
2018-10-22 21:47:35 +00:00
|
|
|
|
|
|
|
! tip: Don't like colors or emoji?
|
|
|
|
|
|
|
|
You can disable colors and emoji by setting the `ROCKET_CLI_COLORS`
|
2021-07-04 23:33:47 +00:00
|
|
|
environment variable to `0` or `false` when running a Rocket binary:
|
|
|
|
`ROCKET_CLI_COLORS=false cargo run`.
|