Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
868 views
in Technique[技术] by (71.8m points)

import - Rust: no `module` in the root

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

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"

Using mod super::

error: expected identifier, found keyword `super`
 --> src/cmdline.rs:2:5
  |
2 | mod super::feed;
  |     ^^^^^ expected identifier, found keyword

Or with use 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I figured it out. The rust module system doesn't permit importing sibling files,

  • src/a.rs
  • src/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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...