无法将函数移动到另一个文件中?
Cannot move function into another file?
我在使用 rocket 库时将函数移动到单独的文件时遇到了一些问题。
我能够编译 运行 来自火箭网页的 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;
|
在这种情况下这是什么意思,我该如何解决?
正如@Jmb 评论的那样,问题是文件 index
和函数 index
混淆了。
所以解决方案是将函数指定为 index::index
.
我在使用 rocket 库时将函数移动到单独的文件时遇到了一些问题。
我能够编译 运行 来自火箭网页的 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;
|
在这种情况下这是什么意思,我该如何解决?
正如@Jmb 评论的那样,问题是文件 index
和函数 index
混淆了。
所以解决方案是将函数指定为 index::index
.