mirror of https://github.com/rwf2/Rocket.git
Initial guide updates for 0.3.
This commit is contained in:
parent
4409a903e3
commit
a6e01f97c1
|
@ -87,7 +87,7 @@ For instance, to mount the `world` route we declared above, we would use the
|
|||
following code:
|
||||
|
||||
```rust
|
||||
rocket::ignite().mount("/hello", routes![world])
|
||||
rocket::ignite().mount("/hello", routes![world]);
|
||||
```
|
||||
|
||||
Altogether, this creates a new `Rocket` instance via the `ignite` function and
|
||||
|
@ -111,7 +111,7 @@ use other::world;
|
|||
|
||||
fn main() {
|
||||
// error[E0425]: cannot find value `static_rocket_route_info_for_world` in this scope
|
||||
rocket::ignite().mount("/hello", routes![world])
|
||||
rocket::ignite().mount("/hello", routes![world]);
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -120,7 +120,7 @@ into the name of a structure generated by Rocket's code generation. The solution
|
|||
is to name the route by a module path instead:
|
||||
|
||||
```rust
|
||||
rocket::ignite().mount("/hello", routes![other::world])
|
||||
rocket::ignite().mount("/hello", routes![other::world]);
|
||||
```
|
||||
|
||||
## Launching
|
||||
|
|
|
@ -211,6 +211,7 @@ use std::io;
|
|||
use std::path::Path;
|
||||
|
||||
use rocket::Data;
|
||||
use rocket::http::RawStr;
|
||||
```
|
||||
|
||||
The [Data](https://api.rocket.rs/rocket/data/struct.Data.html) structure is key
|
||||
|
@ -297,7 +298,7 @@ paste doesn't exist.
|
|||
use std::fs::File;
|
||||
|
||||
#[get("/<id>")]
|
||||
fn retrieve(id: &str) -> Option<File> {
|
||||
fn retrieve(id: &RawStr) -> Option<File> {
|
||||
let filename = format!("upload/{id}", id = id);
|
||||
File::open(&filename).ok()
|
||||
}
|
||||
|
@ -326,7 +327,7 @@ using it. We do this by implementing `FromParam` for `PasteID` in
|
|||
use rocket::request::FromParam;
|
||||
|
||||
/// Returns `true` if `id` is a valid paste ID and `false` otherwise.
|
||||
fn valid_id(id: &str) -> bool {
|
||||
fn valid_id(id: &RawStr) -> bool {
|
||||
id.chars().all(|c| {
|
||||
(c >= 'a' && c <= 'z')
|
||||
|| (c >= 'A' && c <= 'Z')
|
||||
|
|
|
@ -57,7 +57,7 @@ not just the world, we could declare a route and handler like so:
|
|||
|
||||
```rust
|
||||
#[get("/hello/<name>")]
|
||||
fn hello(name: &str) -> String {
|
||||
fn hello(name: String) -> String {
|
||||
format!("Hello, {}!", name)
|
||||
}
|
||||
```
|
||||
|
@ -76,7 +76,7 @@ illustrate varied usage:
|
|||
|
||||
```rust
|
||||
#[get("/hello/<name>/<age>/<cool>")]
|
||||
fn hello(name: &str, age: u8, cool: bool) -> String {
|
||||
fn hello(name: String, age: u8, cool: bool) -> String {
|
||||
if cool {
|
||||
format!("You're a cool {} year old, {}!", age, name)
|
||||
} else {
|
||||
|
@ -85,6 +85,19 @@ fn hello(name: &str, age: u8, cool: bool) -> String {
|
|||
}
|
||||
```
|
||||
|
||||
## Raw Strings
|
||||
|
||||
Rocket provides the `RawStr` type for handling raw strings. When used as a path
|
||||
segment, it is passed directly with no modification. This is used instead of
|
||||
the `&str` type.
|
||||
|
||||
```rust
|
||||
#[get("/hello/<name>")]
|
||||
fn hello(name: &RawStr) -> String {
|
||||
format!("Hello, {}!", name)
|
||||
}
|
||||
```
|
||||
|
||||
## Forwarding
|
||||
|
||||
In this example above, what if `cool` isn't a `bool`? Or, what if `age` isn't a
|
||||
|
@ -107,7 +120,7 @@ fn user(id: usize) -> T { ... }
|
|||
fn user_int(id: isize) -> T { ... }
|
||||
|
||||
#[get("/user/<id>", rank = 3)]
|
||||
fn user_str(id: &str) -> T { ... }
|
||||
fn user_str(id: &RawStr) -> T { ... }
|
||||
```
|
||||
|
||||
Notice the `rank` parameters in `user_int` and `user_str`. If we run this
|
||||
|
@ -125,7 +138,7 @@ be routed as follows:
|
|||
route always matches. The `user_str` handler is called.
|
||||
|
||||
Forwards can be _caught_ by using a `Result` or `Option` type. For example, if
|
||||
the type of `id` in the `user` function was `Result<usize, &str>`, then `user`
|
||||
the type of `id` in the `user` function was `Result<usize, &RawStr>`, then `user`
|
||||
would never forward. An `Ok` variant would indicate that `<id>` was a valid
|
||||
`usize`, while an `Err` would indicate that `<id>` was not a `usize`. The
|
||||
`Err`'s value would contain the string that failed to parse as a `usize`.
|
||||
|
|
|
@ -132,7 +132,9 @@ GitHub](https://github.com/SergioBenitez/Rocket/tree/v0.2.8/examples/json).
|
|||
## Templates
|
||||
|
||||
Rocket has built-in support for templating. To respond with a rendered template,
|
||||
simply return a
|
||||
ensure that you are using
|
||||
[`Template::fairing()`](https://api.rocket.rs/rocket_contrib/struct.Template.html#method.fairing)
|
||||
and then simply return a
|
||||
[Template](https://api.rocket.rs/rocket_contrib/struct.Template.html) type.
|
||||
|
||||
```rust
|
||||
|
@ -141,6 +143,13 @@ fn index() -> Template {
|
|||
let context = /* object-like value */;
|
||||
Template::render("index", &context)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
rocket::ignite()
|
||||
.mount("/", routes![index])
|
||||
.attach(Template::fairing())
|
||||
.launch();
|
||||
}
|
||||
```
|
||||
|
||||
Templates are rendered with the `render` method. The method takes in the name of
|
||||
|
|
Loading…
Reference in New Issue