Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I split crate in multiple files without introducing modules for each file?

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?

like image 816
Christoph Avatar asked Jun 05 '14 22:06

Christoph


1 Answers

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.

like image 108
BurntSushi5 Avatar answered Oct 20 '22 18:10

BurntSushi5