1

I'm trying to define the crate going along with my Rust application as explained in the documentation. Let's take the following directory structure:

src/
├─ lib.rs
├─ main.rs
└─ myapp/
  ├─ a/
  │ ├─ a1.rs
  │ └─ mod.rs
  └─ mod.rs

Now I defined the following in my rust files:

lib.rs

pub mod myapp;

main.rs

extern crate myapp;
use myapp::a;

fn main() {
    unimplemented!();
}

myapp/mod.rs

pub mod a;

myapp/a/mod.rs

pub use self::a1::*;
mod a1;

myapp/a/a1.rs

pub fn myfunc()
{
    unimplemented!();
}

If I try to compile the above directory tree, I got the error:

$ cargo build
Compiling myapp v0.1.0
src/main.rs:2:5: 2:13 error: unresolved import `myapp::a`. There is no `a` in `myapp` [E0432]
src/main.rs:2 use myapp::a;
                  ^~~~~~~~
src/main.rs:2:5: 2:13 help: run `rustc --explain E0432` to see a detailed explanation
error: aborting due to previous error
Could not compile `myapp`.

Where am I wrong here? I think I have the same directory structure as the one shown in the documentation.

Spack
  • 464
  • 4
  • 22

1 Answers1

4

You've added an extra level of nesting. Your main works if you change it to be

extern crate myapp;
use myapp::myapp::a;

fn main() {
    unimplemented!();
}

Crates form their own level in the path, so the first myapp refers to the crate, the second myapp refers to the module you've created.

On a side note, it's rare to see a crate with this level of nesting and bifurcation. Rust crates tend to have larger chunks of code per file, and certainly not the one-struct-per-file that is common in other languages. This is all in my experience, of course, and isn't a hard-and-fast rule.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366