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
|
|
|
|
compatible version of Rust, create a new Cargo project that depends on Rocket,
|
|
|
|
and then run the application.
|
|
|
|
|
|
|
|
## Installing Rust
|
|
|
|
|
2017-07-03 05:51:24 +00:00
|
|
|
Rocket makes abundant use of Rust's syntax extensions and other advanced,
|
2017-12-27 04:50:56 +00:00
|
|
|
unstable features. Because of this, we'll need to use a supported _nightly_
|
|
|
|
version of Rust.
|
2017-04-17 02:48:59 +00:00
|
|
|
|
2017-12-27 04:50:56 +00:00
|
|
|
To install such a version, we recommend using `rustup`. Install `rustup` by
|
|
|
|
following the instructions on [its website](https://rustup.rs/). Once `rustup`
|
|
|
|
is installed, configure Rust nightly as your default toolchain by running the
|
|
|
|
command:
|
2017-04-17 02:48:59 +00:00
|
|
|
|
|
|
|
```sh
|
2017-12-27 04:50:56 +00:00
|
|
|
rustup default nightly-2017-12-21
|
2017-04-17 02:48:59 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
If you prefer, once we setup a project directory in the following section, you
|
|
|
|
can use per-directory overrides to use the nightly version _only_ for your
|
|
|
|
Rocket project by running the following command in the directory:
|
|
|
|
|
|
|
|
```sh
|
2017-12-27 04:50:56 +00:00
|
|
|
rustup override set nightly-2017-12-21
|
2017-04-17 02:48:59 +00:00
|
|
|
```
|
|
|
|
|
2017-07-10 11:59:55 +00:00
|
|
|
### Minimum Nightly
|
2017-04-17 02:48:59 +00:00
|
|
|
|
2017-12-27 04:50:56 +00:00
|
|
|
Rocket generally requires the _latest_ version of Rust nightly. At present,
|
|
|
|
however, a Rocket dependency fails to build on the latest nightlies, temporarily
|
|
|
|
grounding Rocket (see [#513] for details). As a result, you'll need to use the
|
|
|
|
last known working nightly release while the issue is resolved. The commands
|
|
|
|
above already take this into account by installing the `2017-12-21` nightly.
|
|
|
|
|
|
|
|
Once the external issue has been resolved, you can upgrade to the latest nightly
|
|
|
|
with `rustup default nightly` or `rustup override set nightly`. If your Rocket
|
|
|
|
application suddenly stops building, it's likely that this issue has been
|
|
|
|
resolved. If Rocket fails to compile in the future, you should also ensure
|
|
|
|
you're using the latest version of Rust nightly and Rocket by updating your
|
|
|
|
toolchain and dependencies with:
|
2017-04-17 02:48:59 +00:00
|
|
|
|
|
|
|
```sh
|
|
|
|
rustup update && cargo update
|
|
|
|
```
|
|
|
|
|
2017-12-27 04:50:56 +00:00
|
|
|
[#513]: https://github.com/SergioBenitez/Rocket/issues/513
|
|
|
|
|
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
|
|
|
|
```
|
|
|
|
|
|
|
|
Now, add Rocket and its code generation facilities as dependencies of your
|
|
|
|
project by ensuring your `Cargo.toml` contains the following:
|
|
|
|
|
|
|
|
```
|
|
|
|
[dependencies]
|
2017-08-11 16:32:27 +00:00
|
|
|
rocket = "0.4.0-dev"
|
|
|
|
rocket_codegen = "0.4.0-dev"
|
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
|
2017-09-15 05:07:05 +00:00
|
|
|
#![feature(plugin, decl_macro)]
|
2017-04-17 02:48:59 +00:00
|
|
|
#![plugin(rocket_codegen)]
|
|
|
|
|
|
|
|
extern crate rocket;
|
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
fn index() -> &'static str {
|
|
|
|
"Hello, world!"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
rocket::ignite().mount("/", routes![index]).launch();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
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
|
|
|
|
🔧 Configured for development.
|
|
|
|
=> address: localhost
|
|
|
|
=> port: 8000
|
|
|
|
=> log: normal
|
2017-07-10 11:59:55 +00:00
|
|
|
=> workers: [core count * 2]
|
2017-07-03 05:51:24 +00:00
|
|
|
=> secret key: generated
|
|
|
|
=> limits: forms = 32KiB
|
|
|
|
=> tls: disabled
|
2017-04-17 02:48:59 +00:00
|
|
|
🛰 Mounting '/':
|
|
|
|
=> GET /
|
2017-07-03 05:51:24 +00:00
|
|
|
🚀 Rocket has launched from http://localhost:8000
|
2017-04-17 02:48:59 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
Visit `http://localhost:8000` to see your first Rocket application in action!
|