Rocket: Docs: consider adding import statement for types.

Created on 16 Sep 2017  路  5Comments  路  Source: SergioBenitez/Rocket

As a beginner in Rust, I don't know if a type presented in a code example belongs to Rust's stdlib or Rocket. It might be clearer if they are explicitly imported.

For example:
In https://rocket.rs/guide/requests/ Dynamic Segments section:

// Orginal
#[get("/hello/<name>")]
fn hello(name: &RawStr) -> String {
    format!("Hello, {}!", name.as_str())
}

// Becomes
use rocket::http::RawStr;

#[get("/hello/<name>")]
fn hello(name: &RawStr) -> String {
    format!("Hello, {}!", name.as_str())
}
// Orginal
#[get("/page/<path..>")]
fn get_page(path: PathBuf) -> T { ... }

// Becomes
use std::path::PathBuf;

#[get("/page/<path..>")]
fn get_page(path: PathBuf) -> T { ... }

I can implement this if this is a good idea.

accepted docs help wanted request

Most helpful comment

I think we should do this. Perhaps we can have an "expand" button on code snippets to show a fully runnable example, a-la rustdoc. At the minimum, we can simply have the imports at the top of snippets.

All 5 comments

This tripped me up too. It is really nice when examples are fully copy/paste-able

This is the error that you get:

$ cargo run
   Compiling hello-rocket v0.1.0 (/var/www/vhosts/rust/hello-rocket)
error[E0412]: cannot find type `RawStr` in this scope
  --> src/main.rs:16:17

Searching for this error returns mostly cryptic results and certainly no direct answer. Hopefully in the future Googling for the error will pick up this issue. The Google results do bring up https://api.rocket.rs/rocket/http/struct.RawStr.html which correctly suggests the use rocket::http::RawStr; but its frustrating to have to do the run around when trying to get things to work.

Also people learning Rust at the same time as Rocket may well not know about the use syntax which makes it doubly hard. This shouldn't really be a problem for Rocket, but I certainly am using Rocket as a first example of a more extensive Rust project and so I'm less familiar with Rust syntax. I have at least already been reading through the "Programming Rust" book but I expect there will be others who haven't. So where as there is no expectation that Rocket should have to explain fundamentals to users it's nice if the examples do try to be more explicit.

I've also seen that the documentation has been updated to include use std::path::PathBuf; in the Multiple Segments section. So it seems like half of this issue has been fixed.

I think we should do this. Perhaps we can have an "expand" button on code snippets to show a fully runnable example, a-la rustdoc. At the minimum, we can simply have the imports at the top of snippets.

https://rocket.rs/v0.4/guide/requests/

mentions RawStr and what it's for, but not the incantation required to make it actually referencable in your code. when use rocket::http::RawStr; finally does show up in the new version ( f5b6e3d ) it is literally pages worth of scrolling _below_ when you're first told to use it.

@SergioBenitez I would request that you please make the first example of the use rocket::http::RawStr; line be at the same place as the first time a new user is instructed to put RawStr in their code.

@masukomi Feel free to open a PR that implements your suggestion!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

PSeitz picture PSeitz  路  3Comments

ndarilek picture ndarilek  路  3Comments

Ronaldho80 picture Ronaldho80  路  3Comments

kitsuneninetails picture kitsuneninetails  路  4Comments

marceloboeira picture marceloboeira  路  3Comments