【发布时间】:2021-02-21 23:00:21
【问题描述】:
在使用火箭库时,我在将函数移动到单独的文件时遇到了一些问题。
我能够编译和运行来自火箭网页的getting-started-example,如下所示:
// main.rs
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
#[get("/")]
pub fn index() -> &'static str { "Hello, world!" }
fn main() { rocket::ignite().mount("/", routes![index]).launch(); }
但如果我将index() 函数移动到它自己的文件中,在同一目录中,如下所示:
// main.rs
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
mod index;
fn main() { rocket::ignite().mount("/", routes![index]).launch(); }
// index.rs
#[get("/")]
pub fn index() -> &'static str { "Hello, world!" }
然后我会看到错误消息:
error[E0425]: cannot find value `static_rocket_route_info_for_index` in this scope
--> src/main.rs:8:41
|
8 | rocket::ignite().mount("/", routes![index]).launch();
| ^^^^^ not found in this scope
|
help: consider importing this static
|
5 | use crate::index::static_rocket_route_info_for_index;
|
在这种情况下这是什么意思,我该如何解决?
【问题讨论】:
-
index函数和index模块之间存在混淆。您应该use index::index;或写routes![index::index]。
标签: rust rust-rocket