微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法将函数移动到另一个文件中?

如何解决无法将函数移动到另一个文件中?

在使用火箭库时,我在将函数移动到单独的文件时遇到了一些问题。

我能够从火箭网页编译和运行 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。