试图让Rust加载文件 [英] Trying to get Rust to load files

查看:85
本文介绍了试图让Rust加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使Rust加载子目录中的文件时遇到了麻烦. Rust将文件视为模块,将代码视为模块的一部分,但是我已经习惯了Ruby对待文件和目录与其所包含的代码分开的方式.

I'm having trouble trying to get Rust to load files in subdirectories. Rust treats the files as the modules and the code as part of the module, but I'm used to Ruby's way of treating files and directories separate from the code that they contain.

src/main.rs

mod lib {
    pub mod manifest;
}

src/lib/manifest.rs

mod structs {
    pub mod entity;
}

src/lib/structs/entity.rs

pub struct entity {
    type: String
}

我得到的错误是:

error: cannot declare a new module at this location
 --> src/lib/manifest.rs:2:13
  |
2 |     pub mod entity;
  |             ^^^^^^
  |
note: maybe move this module `structs` to its own directory via `structs/mod.rs`
 --> src/lib/manifest.rs:2:13
  |
2 |     pub mod entity;
  |             ^^^^^^
note: ... or maybe `use` the module `entity` instead of possibly redeclaring it
 --> src/lib/manifest.rs:2:13
  |
2 |     pub mod entity;
  |             ^^^^^^

推荐答案

解决此问题的最佳方法是将清单文件重命名为 mod.rs 并更改 main中的第一行.rs 发送至:

The best way to fix this is to rename the manifest file to mod.rs and change the first line in main.rs to:

mod lib;

并将 mod.rs 更改为:

pub mod structs {
    mod entity;
}

我认为您出错的原因是因为有一个 manifest.rs 文件,但没有文件夹.为什么这会导致子文件夹文件无法加载,我不能说,我还是Rust的新手,所以很有可能我错了.

I think the reason for your error is because there's a manifest.rs file but no folder. Why this causes the subfolder files to not load I can't say, I'm still new to Rust myself so there's a good chance I'm wrong.

一些具有更多Rust知识的人可能知道,但是由于他们不赞成而不是有所帮助,所以我怀疑您会得到更好的答案.

Someone with more Rust knowledge might know, but since they're downvoting instead of being helpful, I doubt you'd get a better answer.

这篇关于试图让Rust加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆