I love β€οΈ rust but I hate π how vague the beginner documentation is about splitting up your project into a practical structure. This is how I do it (for a library project anyway):
.
βββ project/
βββ src/
β βββ lib.rs
β βββ top_level_module.rs
β βββ util/
β βββ mod.rs
β βββ utility_module_1.rs
β βββ utility_module_2.rs
β βββ private_module.rs
βββ cargo.toml
βββ readme.md
mod util;
mod top_level_module;
use util::utility_module_1::{Foo, Bar}
use util::utility_module_1::{Spam, Eggs}
use util::Sneaky;
use top_level_module::Stuff
// Do Stuff
// note: no need for `mod` since that appears in lib.rs
use util::utility_module_1::{Foo, Bar}
pub struct Stuff{
pub ff:Foo,
pub bb:Bar,
// ...
}
// we can choose to export sub modules as public submodules here
pub mod utility_module_1.rs
pub mod utility_module_2.rs
// we can also re-export the contents of submodules
// as if they were defined in the `util` module
mod private_module;
pub use private_module::Sneaky;
pub struct Foo(u8);
pub struct Bar(f64);
// submodules can use code from eachother by pathing from the `crate::` root:
use crate::util::utility_module_1::Foo;
// OR
// they can refer to the module above using `super::`
// in this case super:: refers to util
use super::utility_module_1::Foo;
pub struct Spam();
pub struct Eggs{
pub aa:Foo
};
pub struct Sneaky();
My pleasure, glad to have helped! :)