APW
APW

Reputation: 537

how to layout my Rust modules in src for access across my application

I have created a package structure but the compiler says it can't find my module. I am new to rust, with a background in Java & C#

I have tried using self and super prefixes but can't get the code to compile

I have the following structure:

src
|_lib.rs
|_common
| |_mod.rs
| |_service.rs
|
|_animals
  |_mod.rs
  |_domestic
  | |_mod.rs
  | |_dog.rs
  |_wild
  |_mod.rs

here are the simplified files:

dog.rs

pub struct Dog {
  ...
}

impl Dog {
    ...
}

domestic > mod.rs

pub mod dog;

animals > mod.rs

pub mod domestic;
pub mod wild;

src > lib.rs

pub mod common;
pub mod animals;

common > service

use animals::domestic::dog;

From what I have read (and possibly misunderstood) in the Rust book, this should work.

But the compiler throws the following error:

could not find `animals` in `{{root}}`

Have I set this out in a 'Rust' way? and what do I need to change to get it to compile.

Thank you

Upvotes: 0

Views: 194

Answers (1)

Bruno Grieder
Bruno Grieder

Reputation: 29884

In commons > service

use crate::animals::domestic::dog;

The crate keyword tells the compiler to start from the root of the package; i.e. what follows is an absolute path.

Alternatively, you can go with relative paths and use the super keyword to go one level up.

See this Rust book entry for details

Upvotes: 2

Related Questions