8

All examples that I found create a lib.rs and then inside that file create mod foo;, mod bar; for each file foo.rs, bar.rs and so on.

Basically what I want is to to split my crate into multiple files but without having to introduce lots of modules for every file. Is this not possible with Rust?

Chris Morgan
  • 86,207
  • 24
  • 208
  • 215
Christoph
  • 26,519
  • 28
  • 95
  • 133

1 Answers1

12

In order to include another source file, it has to be its own module.

However, this does not mean that you must expose this fact in your API. For example, let's say you want to put some code in a file called hidden.rs:

pub fn inside_hidden() {}

Now in your lib.rs file, you could expose this module:

#![crate_type = "lib"]

pub mod hidden;

But if your library doesn't require sub-modules, it's probably better to keep the hidden module a secret, and instead expose its contents as members of your top-level module:

#![crate_type = "lib"]

pub use hidden::inside_hidden;

mod hidden;

This way, users won't know you have a hidden sub-module. They will see inside_hidden as if it were a member of your top-level module.

BurntSushi5
  • 13,917
  • 7
  • 52
  • 45