mirror of
https://github.com/m-lamonaca/dev-notes.git
synced 2025-04-08 11:56:41 +00:00
Fix modules notes and formatting
This commit is contained in:
parent
9e59eeeea4
commit
dd447cfbcc
1 changed files with 27 additions and 16 deletions
|
@ -80,7 +80,9 @@ crate::module::function(); // abs path (same crate)
|
||||||
|
|
||||||
### Public vs Private
|
### Public vs Private
|
||||||
|
|
||||||
Modules aren’t useful only for organizing the code. They also define Rust’s privacy boundary: the line that encapsulates the implementation details external code isn’t 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 aren’t useful only for organizing the code. They also define Rust’s privacy boundary:
|
||||||
|
the line that encapsulates the implementation details external code isn’t 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 can’t 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 can’t 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 struct’s 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 struct’s 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
|
||||||
```
|
```
|
||||||
|
|
Loading…
Add table
Reference in a new issue