Rust:根目录中没有模块 [英] Rust: no `module` in the root
问题描述
当我在 src/cmdline.rs
中运行 use crate :: feed;
时,我希望导入 src/feed.rs
,但事实并非如此.相反,我明白了,
When I run use crate::feed;
in src/cmdline.rs
I expect that to import src/feed.rs
, but it doesn't. Instead I get,
error[E0432]: unresolved import `crate::feed`
--> src/cmdline.rs:2:5
|
2 | use crate::feed;
| ^^^^^^^^^^^ no `feed` in the root
尽管存在 src/feed.rs
的事实.但是,如果我感到恐慌并将其更改为 mod feed;
,那么我会得到
Despite the fact that src/feed.rs
exists. However, if I panic and change it to mod feed;
then I get
error[E0583]: file not found for module `feed`
--> src/cmdline.rs:2:1
|
2 | mod feed;
| ^^^^^^^^^
|
= help: to create the module `feed`, create file "src/cmdline/feed.rs"
使用 mod super ::
error: expected identifier, found keyword `super`
--> src/cmdline.rs:2:5
|
2 | mod super::feed;
| ^^^^^ expected identifier, found keyword
或与一起使用super ::
error[E0432]: unresolved import `super::feed`
--> src/cmdline.rs:2:5
|
2 | use super::feed;
| ^^^^^^^^^^^ no `feed` in the root
有问题的文件的文件结构如下:
File structure for files in question looks like this,
src/feed.rs
src/cmdline.rs
src/main.rs
推荐答案
我知道了.rust模块系统不允许导入同级文件,
I figured it out. The rust module system doesn't permit importing sibling files,
-
src/a.rs
-
src/b.rs
完全停止: a.rs
无法导入 b.rs
.它将要做的是尝试从中获取
Full stop: a.rs
can not import b.rs
. What it will do is try to source it from
-
src/a/b.rs
如果您正在寻找答案,那么这对您来说可能都不有意义,并且您已经在此上浪费了数小时.这是我感到困惑的原因:
If you're on this answer, none of this probably makes sense to you and you've wasted hours on this. This was a source of my confusion:
-
src/main.rs
实际上很特别.在 src/main.rs
内,一个 mod
将导入一个同级文件(以及现在已弃用的 mod.rs
;或者,使用 lib.rs
).但要点是,您自己的锈文件不能在同级文件中使用锈代码.
Is actually special. Inside src/main.rs
a mod
will import a sibling file, (and also with the now deprecated mod.rs
; or, with lib.rs
). But the point is that your own rust files can't make use of rust code in sibling files.
这篇关于Rust:根目录中没有模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!