5

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
Jason
  • 4,905
  • 1
  • 30
  • 38
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468

2 Answers2

7

I was able to make my imports work the way you describe by doing the following.

First in main.rs I import the module cmdline and each module I want to be able to use via crate::.

// file src/main.rs
mod cmdline;
mod feed; // <== import module to be able to use via `crate::feed`

fn main() {
  cmdline::do_something();
}

Then in cmdline.rs I use create::feed.

// file src/cmdline.rs

use crate::feed; // <== import sibling module

pub fn do_something() {
  feed::do_something_else();
}

And my feed.rs looks something like this.

// file src/feed.rs
pub fn d_something_else() {
  // ...
}

From what I understand from experimenting is that you need to first use mod in your main.rs to define which modules are included in your crate.

dgellow
  • 692
  • 1
  • 11
  • 18
6

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.

Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
  • `main.rs` is not special in that sense. Rust builds a module hierarchy with the provided `mod _;` usages. When you try to `use crate::feed`, it looks into `main` module (since it's the crate root), and checks if a module named `feed` is connected to it. Since it's not (as you didn't include the `mod feed;` in main.rs), the compiler errors out. I will say that though the module system is very clear and not that hard (only initially) to understand, the explicit creation of module hierarchy has not been of use to me till date, as I just mimic the actual file hierarchy. – zombiesauce Nov 01 '21 at 15:26