Binary package: access submodules in examples

I have a binary project with several submodules and an example folder with some examples:

|__ mod_a/
| |__
|__ mod_b/
| |__
| __ examples/
| |__

From the examples folder I cannot access to neither mod_a nor mod_b. I tried to use keywords like crate, extern crate to no avail.
I can guess this is only available for lib projects, is there any workararound for binary projects?

You have to properly integrate your examples folder into the module structure of your crate. I.e. add a examples/ file with:

mod ex1;

and in your file add:

mod examples;

Then you can import mod_a in examples/

use crate::mod_a;

If you want examples like they are supported by Cargo, note that you need to put the examples folder at the top-level of your crate (above src/) (see here) and add a library to your package. The latter is as easy as adding a file next to your, where you define the modules instead of in

pub mod mod_a;
pub mod mod_b;

and change the imports in your to start with your crate name:

use your_crate::mod_a;

If you do that you can access the public items from your library in your examples/, like you'd do in (examples are compiled to binaries, just like

1 Like

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.