"Maybe a missing crate" error when creating a mutliple files example

Hi, I'm trying to create an example that consists of multiple files, the structure is the next:

examples
--fireworks
----app.rs
----firework.rs
----main.rs

app.rs contains a pub trait App and in the firework.rs I'm trying to implement this trait. But I don't know how to refer to the App trait, what path to it is correct, and I get the error maybe a missing crate 'app'.
It was quite easy to use the Firework struct in the main.rs, I just use crate::firework::Firework; but that doesn't work for the app.rs file neither in main.rs nor in firework.rs file.
I tried a dozen different methods like creating a mod.rs and declaring pub mod app; inside, renaming files and example directory, creating a separate directory for the app.rs etc. but none of them worked.

I know the examples directory is not the best place for the App trait, I'll move it to the src soon but for now I'm just interested in how to make this particular case work.

Appreciate for help :slight_smile:

1 Like

Have you read Paths for Referring to an Item in the Module Tree chapter in The Book? It explains how to refer to items from other modules.

The Defining Modules to Control Scope and Privacy chapter also describes how modules can be declared.

I have, but that was a while ago, I'll check it now again, thanks

For those who may face the same issue in future: to use the module you need to declare it first with the mod ****; or pub mod ****; statement. And if you have a directory called firework next to your main.rs or lib.rs file that means you need to declare a mod firework; and this directory must contain the mod.rs file where you can declare or implement public modules, functions etc.

I might be still missing something as the mod.rs file is not mentioned in this chapter Separating Modules into Different Files - The Rust Programming Language but now I need to go, will update the description in case of mistakes later

./src/firework.rs and ./src/firework/mod.rs do the same thing, as of Rust 2018. I think the former is considered a little more idiomatic these days.

Note that a ./src/firework.rs and a ./src/firework/my_submodule.rs can co-exist.

Yes, definitely. In case there's a ./src/firework.rs I can just declare it in the main.rs or lib.rs. But if I want to group several modules in a folder, as far as I know, that folder must contain a mod.rs file, am I right?

That used to be the case, but since Rust 2018, the following directory layouts are treated the same:

src/
    main.rs     (contains the 'mod firework' declaration)
    firework.rs (contains the 'mod my_submodule' declaration)
    firework/
        my_submodule.rs
src/
    main.rs      (contains the 'mod firework' declaration)
    firework/
        mod.rs   (contains the 'mod my_submodule' declaration)
        my_submodule.rs

This saves you from having to move/rename a module if you decide to add submodules to it later on. The fact this was changed is probably why there's not much mention of mod.rs in the current version of the book :slight_smile:

1 Like

Ahh, I understood finally, that's really handy :slight_smile: Thanks for your explanations, now I'm going to get rid of all mod.rs files from my project :grinning:

UPD: if I don't want to keep all mod files directly in the src directory then I'll still need to use mod.rs, won't I? There will be too many files in the src then

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.