4

我想在一个模块中启动 Rocket main(),因此可以简化main()但我失败了。我从Rocket修改了Quicktart

编码:

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();
}

错误:

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

我不知道如何解决它。

4

1 回答 1

6

我达到了。我项目的箱子是rocket_demo

main.rs

extern crate rocket_demo; 

use rocket_demo::my_rocket;

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

库文件

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

pub mod my_rocket;

前三行不能在my_rocket/mod.rs,否则routes!会找不到!

我的火箭/mod.rs

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

pub fn startup() {
    ::rocket::ignite().mount("/", routes![index]).launch();
}
于 2018-02-20T13:30:50.253 回答