Fix modules notes and formatting

This commit is contained in:
Marcello 2021-08-13 19:00:10 +02:00
parent 9e59eeeea4
commit dd447cfbcc

View file

@ -80,7 +80,9 @@ crate::module::function(); // abs path (same crate)
### Public vs Private ### Public vs Private
Modules arent useful only for organizing the code. They also define Rusts privacy boundary: the line that encapsulates the implementation details external code isnt allowed to know about, call, or rely on. So, to make an item like a function or struct private, put it in a module. Modules arent useful only for organizing the code. They also define Rusts privacy boundary:
the line that encapsulates the implementation details external code isnt allowed to know about, call, or rely on.
So, to make an item like a function or struct private, put it in a module.
The way privacy works in Rust is that all items (functions, methods, structs, enums, modules, and constants) are *private by default*. The way privacy works in Rust is that all items (functions, methods, structs, enums, modules, and constants) are *private by default*.
Items in a parent module cant use the private items inside child modules, but items in child modules can use the items in their ancestor modules. Items in a parent module cant use the private items inside child modules, but items in child modules can use the items in their ancestor modules.
@ -100,7 +102,8 @@ mod file_level_module; // define a module for the whole file (same name as file
``` ```
It's possible to use `pub` to designate *structs* and *enums* as public, but there are a few extra details. It's possible to use `pub` to designate *structs* and *enums* as public, but there are a few extra details.
If `pub` is used before a struct definition, this makes the struct public, but the structs fields will still be private. It's possible make each field public or not on a case-by-case basis. If `pub` is used before a struct definition, this makes the struct public, but the structs fields will still be private.
It's possible make each field public or not on a case-by-case basis.
In contrast, if an enum is made public, all of its variants are then public. In contrast, if an enum is made public, all of its variants are then public.
@ -108,8 +111,8 @@ In contrast, if an enum is made public, all of its variants are then public.
```rs ```rs
use <crate_name>::module; // import module (abs path, other crate) use <crate_name>::module; // import module (abs path, other crate)
use crate::module; // s (abs path, same crate) use crate::module; // import module (abs path, same crate)
use self::module; // // import module (rel path, same crate) use self::module; // import module (rel path, same crate)
use <crate_name>::module as alias; // import module w/ aliass use <crate_name>::module as alias; // import module w/ aliass
pub use <crate_name>::module; // re-exporting (import and make available to others) pub use <crate_name>::module; // re-exporting (import and make available to others)
@ -124,9 +127,17 @@ module::function(); // use func w/ shorter path
```txt ```txt
src src
|_main.rs --> app entrypoint |_main.rs --> default executable file
|_lib.rs --> export module-folders |_lib.rs --> default library file
|__module |__module
| |_mod.rs --> export module-files | |_mod.rs --> export submodules
| |_file.rs | |_submodule.rs --> submodule
```
```rs
// main.rs
mod module; // delcare module direcotry as a module
// mod.rs
pub mod sub_module; // declare sub_module file as a module
``` ```