Reputation: 8315
Since the documentation on that is a bit vast, I wanted just to tackle a single problem with modules
I have the following files
project/src/main.rs
project/src/win/mod.rs //just some win32 specific utilities
project/src/geom/mod.rs
project/src/geom/rectangle.rs
project/src/geom/triangle.rs
As long as there is just the mod.rs
(like with the folder win
), I have no problems
pub fn enumerate_monitors() -> Vec<MONITORINFOEXW>
mod win;
somewherelet monitors = win::enumerate_monitors();
But that was a small file, so no real problems, then I'm starting to add much code inside geom
, I can't add that all inside mod.rs
How can I add all shapes to geom
module?
mod geom;
fn main()
{
let tri = geom::triangle::new();
let rect = geom::rectangle:new();
}
Is it also possible to keep a mod.rs
inside the geom
folder in order to use that to expose all shapes?
Upvotes: 1
Views: 2377
Reputation: 2760
if it's a library you are looking to create (and not an executable crate) there must be at least a src/lib.rs
file. That file most times has no main()
and it's usual to "import" the various sub-modules in there.
In your case, you could bring in scope other modules with something like this:
// For example in project/src/geom/mod.rs
// bring rectangle and triangle in the scope of the current module:
pub mod rectangle;
pub mod triangle;
After this whatever other file use
s geom/mod.rs also has access to functions / structs defined in rectangle.rs
and can access them using something like rectangle::<struct-or-function-name
Additionally, you can also use something like pub use self::rectangle::*
:
pub mod rectangle;
pub mod triangle;
use self::rectangle::*;
use self::triangle::*;
This brings all the functions / structs etc. in the scope of geom/mod.rs
. This way whatever other module uses geom/mod.rs
won't have to specify e.g., the extra rectangle::
scope before accessing its structs and functions.
Upvotes: 3