0

I want to organize some functions into multiple files since they're big enough to warrant that but from outside of that directory I want them all to appear to be exported from same parent module.

I have the files:

/main.rs
/example/mod.rs
/example/a.rs
/example/b.rs

From within main.rs, I want to be able to do:

use example::{a, b};

Both a.rs and b.rs contain a single function:

/example/a.rs

pub fn a() { }

/example/b.rs

pub fn b() { }

I can't figure out how to export them from /example/mod.rs such that it appears to be all in the example module.

I can do this:

/example/mod.rs

pub mod a;
pub mod b;

But then my use later looks like:

/main.rs

use example::a::a;
use example::b::b;

// How do I do this?
// use example::{a, b};

EDIT: The solution is to have /example/mod.rs look like this:

pub use a::a;
pub use b::b;

mod a;
mod b;
justin.m.chase
  • 13,061
  • 8
  • 52
  • 100
  • It looks like your question might be answered by the answers of [How to have multiple files with one module?](https://stackoverflow.com/q/42220379/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Jan 01 '20 at 17:18
  • [The duplicate applied to your case](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f2f478e55347388757542fb273b3e728) – Shepmaster Jan 01 '20 at 17:23
  • It doesn't work and the answer in the other question doesn't address it either... Or perhaps it does but its not obvious how to make it work across multiple files to someone not familiar. These examples show how to import and define the module all in the same file, but the question is specifically about multiple files, which is apparently done differently. – justin.m.chase Jan 01 '20 at 18:51
  • Placing modules in one file or multiple doesn't change anything materially for this situation. Here's [the explicit file organization](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=a310f9c2674a69853596b633460ed074), but you may wish to re-read [Separating Modules into Different Files](https://doc.rust-lang.org/book/ch07-05-separating-modules-into-different-files.html) which discusses the process to transform from the original answer. – Shepmaster Jan 01 '20 at 18:55
  • Ok, thank you! The trick was having a `mod a;` in `example/mod.rs`. When I was just doing a `pub use a::a` it was giving me an error saying `use of undeclared type or module 'a'` and that was what was confusing me. Adding that simple mod statement adds the reference and solves it. – justin.m.chase Jan 01 '20 at 19:04

0 Answers0