Putting all declarations in single file


I’ve an app that is using the below declaration:

use std::ffi::{CString, CStr};

pub mod ios { }
pub mod android { }
pub mod wasm { }

And I need to split these mods into separate files: android.rs, ios.rs and wasm.rs.

My folder tree now is:

Hasans-Air:src h_ajsf$ tree .
├── android.rs
├── ios.rs
├── lib.rs
└── wasm.rs

0 directories, 4 files

Do you need to repeat the mentioned declaration in each and every file, I read mod-and-the-filesystem but looks did not get how to manage this.


The contents of a module work the same way, regardless whether it’s an inline mod {} or in a separate file. Declarations in the top level of lib.rs have the same visibility to the modules either way.


No, you don’t have to repeat the same declaration in each file. In your lib.rs file, declare your modules somewhere up top like this:

pub mod android; // represents "android.rs"
pub mod ios; // represents "ios.rs"
pub mod wasm; // represents "wasm.rs"

Then in each of your new module files, leave out the mod statement. For example, you don’t need to declare pub mod android { } in android.rs.

Another nifty trick you can do is instead of a having file named android.rs, create a folder called android with a file called mod.rs inside it, then put all the declarations for the android module in that mod.rs file. This allows you to have submodules as other files within a directory structure, like so: