From a6e01f97c1eb4bb92813fc0fc9b658d1c8679360 Mon Sep 17 00:00:00 2001 From: Lori Holden Date: Tue, 6 Jun 2017 13:23:46 -0400 Subject: [PATCH] Initial guide updates for 0.3. --- site/guide/overview.md | 6 +++--- site/guide/pastebin.md | 5 +++-- site/guide/requests.md | 21 +++++++++++++++++---- site/guide/responses.md | 11 ++++++++++- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/site/guide/overview.md b/site/guide/overview.md index 6b2e1ec6..1149dfd0 100644 --- a/site/guide/overview.md +++ b/site/guide/overview.md @@ -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 diff --git a/site/guide/pastebin.md b/site/guide/pastebin.md index bbe522c6..58816aee 100644 --- a/site/guide/pastebin.md +++ b/site/guide/pastebin.md @@ -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("/")] -fn retrieve(id: &str) -> Option { +fn retrieve(id: &RawStr) -> Option { 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') diff --git a/site/guide/requests.md b/site/guide/requests.md index 7dce07b1..4f8e8a0d 100644 --- a/site/guide/requests.md +++ b/site/guide/requests.md @@ -57,7 +57,7 @@ not just the world, we could declare a route and handler like so: ```rust #[get("/hello/")] -fn hello(name: &str) -> String { +fn hello(name: String) -> String { format!("Hello, {}!", name) } ``` @@ -76,7 +76,7 @@ illustrate varied usage: ```rust #[get("/hello///")] -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/")] +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/", 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`, then `user` +the type of `id` in the `user` function was `Result`, then `user` would never forward. An `Ok` variant would indicate that `` was a valid `usize`, while an `Err` would indicate that `` was not a `usize`. The `Err`'s value would contain the string that failed to parse as a `usize`. diff --git a/site/guide/responses.md b/site/guide/responses.md index b381cbea..86cc1024 100644 --- a/site/guide/responses.md +++ b/site/guide/responses.md @@ -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