2

I am a newbie of rust, I am learning rust now. When I write code like this in rust:

#[macro_use] 
extern crate rocket;
extern crate diesel;

#[get("/hello")]
fn index() -> &'static str {
    "Hello, world!"
}

#[launch]
fn rocket() -> _ {
   rocket::build()
    .mount("/hello", routes![index])
}

it works fine, but when I swich the extern import sequence:

#[macro_use] 
extern crate diesel;
extern crate rocket;

#[get("/hello")]
fn index() -> &'static str {
    "Hello, world!"
}

#[launch]
fn rocket() -> _ {
   rocket::build()
    .mount("/hello", routes![index])
}

I switch the rocket and the diesel position, the rocket switched to second postion, and the cargo build output look like this:

~/Documents/GitHub/reddwarf_music on  develop! ⌚ 10:10:04
$ cargo build                                                                                                                              ‹ruby-2.7.2›
   Compiling reddwarf_music v0.1.0 (/Users/dolphin/Documents/GitHub/reddwarf_music)
error: cannot find attribute `launch` in this scope
  --> src/main.rs:10:3
   |
10 | #[launch]
   |   ^^^^^^

error: cannot find macro `routes` in this scope
  --> src/main.rs:13:22
   |
13 |     .mount("/hello", routes![index])
   |                      ^^^^^^
   |
   = note: consider importing this macro:
           rocket::routes

error: cannot find attribute `get` in this scope
 --> src/main.rs:5:3
  |
5 | #[get("/hello")]
  |   ^^^

warning: unused `#[macro_use]` import
 --> src/main.rs:1:1
  |
1 | #[macro_use] 
  | ^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0601]: `main` function not found in crate `reddwarf_music`
  --> src/main.rs:1:1
   |
1  | / #[macro_use] 
2  | | extern crate diesel;
3  | | extern crate rocket;
4  | |
...  |
13 | |     .mount("/hello", routes![index])
14 | | }
   | |_^ consider adding a `main` function to `src/main.rs`

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
  --> src/main.rs:11:16
   |
11 | fn rocket() -> _ {
   |                ^
   |                |
   |                not allowed in type signatures
   |                help: replace with the correct return type: `Rocket<Build>`

error: aborting due to 5 previous errors; 1 warning emitted

Some errors have detailed explanations: E0121, E0601.
For more information about an error, try `rustc --explain E0121`.
error: could not compile `reddwarf_music`

To learn more, run the command again with --verbose.
(base) 

why did this happen? This problem make me confusing. This is my Cargo.toml:

[package]
name = "reddwarf_music"
version = "0.1.0"
edition = "2018"

[dependencies]
rocket = { version = "0.5.0-rc.1", features = ["json"] }
rand = "0.8.4"
serde = { version = "1.0.64", features = ["derive"] }
serde_json = "1.0.64"
reqwest = "0.11.4"

# database
diesel = { version = "1.4.4", features = ["postgres"] }
dotenv = "0.15.0"

1 Answer 1

4

Attributes like #[macro_use] apply to the item immediately after them. So this:

#[macro_use] 
extern crate rocket;
extern crate diesel;

means apply the macro_use attribute to rocket, while this:

#[macro_use] 
extern crate diesel;
extern crate rocket;

means apply the macro_use attribute to diesel.

If you want to apply macro_use to both:

#[macro_use] 
extern crate diesel;

#[macro_use]
extern crate rocket;

Not the answer you're looking for? Browse other questions tagged or ask your own question.