Sibling modules and traits

Trying to make sure I understand this ... finally. Here is a working example where I don't understand why some lines are needed.

Here is src/

pub trait Printable {
    fn print(&self);

Here is src/

// Why do I not need "mod printable;" here?

// Why do I need "crate::" on the next line, but I don't need it in
// the "use printable::Printable" that appears in src/main.js below?
use crate::printable::Printable;

pub struct Person {
    pub first_name: String,
    pub last_name: String,

impl Printable for Person {
    fn print(&self) {
        println!("{} {}", self.first_name, self.last_name);

Here is src/

mod person;
use person::Person;

// I think I need this in order to have the next line,
// but I didn't need this in src/
mod printable;

// Why do I need this at all? See my question at the bottom.
use printable::Printable;

fn main() {
    let p = Person {
        first_name: "Mark".to_string(),
        last_name: "Volkmann".to_string(),

My biggest question is why a trait needs to be in scope in order to call its methods. It seems like it should be enough to have the Person struct in scope because its definition is what provides the implementation of the print method. Is there a technical reason why the Rust compiler cannot support this?

Rust only goes looking in the filesystem for source files that it’s explicitly requested to. You make that request via the mod printable; statement, which creates a new entry in the module tree when it’s processed by the compiler.

The module tree for this example looks like this:

  • crate (
    • crate::printable (
    • crate::person (

This is entirely defined by where the corresponding mod statements appear, and not where the source files happen to be: You’d have exactly the same module tree if you included all of the code from and inline in mod name { ... } blocks.

Paths that don’t start with crate::, super::, or :: are relative to the module in which they appear. Because this statement is inside the crate::person module, you need to either traverse up a level with super:: or start from the module tree root with crate:: to access the sibling module. Otherwise, you’d be trying to access the crate::person::printable module, which doesn’t exist. Inside, you’re inside crate directly so there’s no need for the explicit path, though crate::printable will work there too.

There needs to be exactly one mod printable; statement in your project1. It instructs the compiler to read in as a module defined here.

Rust allows other crates and modules to also define traits that have a print() method and implement them for Person. This rule is to prevent code breaking with an ambiguity when you link in some crate that does this: the code knows which print() method you mean because it’s the one in scope.

1 See @arnaudgolfouse‘s comment for a clarification.


A big thing I was missing is that in my example person and printable are siblings, but neither of those is a sibling of main. I had no idea that was the case!

Would you say that in general mod statements should all go in the top file ( or

Note, the following post does not apply to edition = "2015", but rather, the 2018 edition and onwards

The idea, which has been witnessed to be counter-intuitive when starting, is that the file hierarchy does not exactly match the module hierarchy, especially at the "beginning" of the src/ directory.

I'm gonna try an alternative way of explaining it: imagine your src/ was actually an file.

It represents the root of the code in your (binary) crate, and can have submodules defines within the src/ directory.

Then, a submodules would "always" a, which can, in and of itself, have nested submodules:

In your case:

  • your printable and person submodules do not appear to feature submodules of their own;

  • does not actually exist, it is, instead, named src/ (for a (directly executable) binary / an application), or named src/ for a library.

With this, it should be now clear that src/ sits above the other two "files" / modules, which are "siblings" / of the same depth.

Another way of representing this file hierarchy is using the naming convention:

src/ (in the case of src, it's actually or

This view, although a bit more cumbersome, has the advantage of better mapping the Rust paths to the file paths:

  • the root of the paths is src/, which contains the items defined in that "src module" / crate root.
    In rust, such root is written as crate::

    We have, for instance, the items:

    • crate::main, a function, defined in src/ src/

    • crate::printable, a module, i.e., something that can contain items inside: crate::printable::some_item. Such items are defined in src/printable/ (or src/ for the other file naming convention).

    • crate::person, anoter module.

Now, imagine, within such a file system, that you are inside the /printable/ directory, and that you want to refer to a file within the /person/ directory. If you used a non-qualified path, such as person/... (the equivalent of use person::... in Rust), that would be looking for a person directory (module) nested within your current directory (current module). And you don't have a src/printable/person/... file structure, so that would fail.

You have, on the other hand, two approaches to refer to the contents of that sibling-in-parent-dir directory:

  • either you use the ../person path (in Rust: use super::person::...),

  • or, since in this instance, the parent .. (super) is actually the root of the paths / (crate::), you can use that absolute path:

    /person/... (in Rust: crate::person::...)

And when you are within the "src module" = root-of-the-crate module (src/ src/ file), you can still use both approaches:

  • either the relative path person/... which leads to use person::... in Rust;

  • or the absolute path /person/... wich would lead, in Rust, to use crate::person::...

Since the former is shorter, we thus usually elide the unnecessary / redundant absolute path specified.

I hope this clarifies the pathing question. It turns out that it's the "ergonomic" shortcuts (naming src/ as src/ and being allowed to use instead of module_name/ which make visualizing the module hierarchy not obvious (e.g., src/{main,printable,person}.rs being in the same directory).


This is because modules are not "auto-included", even if there are .rs within the src/ directory: you have to "acknowledge" their existence by declaring them within the parent module contents, by using that mod modname; line.

This is by design, to avoid "action at a distance" when dealing with a complex program structure and libraries:

  • Imagine you have a colleague working with some type, say, a foo: Foo and calling foo.some_method() on it.

  • Now imagine that you, on your own different module, define a new trait,

    trait SomeTrait {
        fn some_method (&self) ...

    and decide that it would be convenient for you that Foos implement your helper trait:

    impl SomeTrait for Foo { ... }
  • If the implemented traits weren't required to be in scope, you'd have introduced a method resolution ambiguity, or worse, shadowed an actual method call, within your colleague's code. If your colleague intended to call <Foo as path::to::SomeTrait>::some_method() because they were aware of your code, then they can either use that fully-qualified method call, or they can use path::to::SomeTrait;. Both ways show that your colleague was aware for the existence of SomeTrait / they have explicitly opted into using that part of your code, so there are no suprises this way :slightly_smiling_face:


That could be misleading: you can have

  • In
    mod printable;
    fn main() {}
  • In printable/
    mod printable;
  • And a printable/ file.

In the same way that the same file name can appear multiple times in the filesystem.


I would say no... Modules are a way to partition the logic of your crate.

If, say, you have a printable module that is supposed to print stuff; and it already contains functions to print text, and floats, and integers... you might want to partition all of that in different files, that each would hold one of the printing function.
But these functions are, overall, all part of the 'printing' stuff, so it could make sense to have something like:

- crate root (
  - printable (printable/
    - print_int   (printable/
    - print_float (printable/
    - print_str   (printable/
  - other modules...

Here, would contain

mod printable;
// other modules ...

and printable/

mod print_int;
mod print_float;
mod print_str;

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.