【问题标题】:Cannot move function into another file?无法将函数移动到另一个文件中?
【发布时间】: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


【解决方案1】:

正如@Jmb 所说,问题在于文件index 和函数index 混淆了。 所以解决办法是把函数指定为index::index

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-02
    • 2014-12-04
    • 2021-11-12
    • 2011-04-28
    • 1970-01-01
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多