4

I want to start Rocket in a module out of main(), thus can simplify main() but I failed. I modified the Quicktart from rocket

The code:

mod myRocket {
    #![feature(plugin)]
    #![plugin(rocket_codegen)]
    extern crate rocket;

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

    pub fn startup() {
        rocket::ignite().mount("/", routes![index]).launch();
    }
}


fn main() {
    myRocket::startup();
}

The error:

error: cannot find macro `routes!` in this scope
--> src\main.rs:12:37
|
12 |         rocket::ignite().mount("/", routes![index]).launch();
|

I don't know how to fix it.

dtolnay
  • 9,621
  • 5
  • 41
  • 62
llxxbb
  • 1,241
  • 1
  • 10
  • 16

1 Answers1

6

I achieved. My project's crate is rocket_demo

main.rs

extern crate rocket_demo; 

use rocket_demo::my_rocket;

fn main() {
    my_rocket::startup();
}

lib.rs

#![feature(plugin)]
#![plugin(rocket_codegen)]
extern crate rocket;

pub mod my_rocket;

The first three lines can't be in my_rocket/mod.rs, otherwise routes! will not find!

my_rocket/mod.rs

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

pub fn startup() {
    ::rocket::ignite().mount("/", routes![index]).launch();
}
llxxbb
  • 1,241
  • 1
  • 10
  • 16