finished ch7

This commit is contained in:
darkicewolf50 2025-01-17 12:37:26 -07:00
parent fc4becb1a0
commit 40a722d93d
3 changed files with 72 additions and 4 deletions

View File

@ -34,9 +34,23 @@
"icon": "lucide-file",
"title": "Modules and Use"
}
},
{
"id": "b6a35c226bb40634",
"type": "leaf",
"state": {
"type": "markdown",
"state": {
"file": "Collection of Common Data Structs.md",
"mode": "source",
"source": false
},
"icon": "lucide-file",
"title": "Collection of Common Data Structs"
}
}
],
"currentTab": 1
"currentTab": 2
}
],
"direction": "vertical"
@ -178,15 +192,16 @@
"command-palette:Open command palette": false
}
},
"active": "b80f5219fa24358f",
"active": "b6a35c226bb40634",
"lastOpenFiles": [
"Modules and Use.md",
"Collection of Common Data Structs.md",
"Packages.md",
"data_types.md",
"Crates.md",
"Project Organization.md",
"crates.io.md",
"Modules and Use.md",
"Paths.md",
"data_types.md",
"does_not_compile.svg",
"Structures.md",
"Primitives.md",

View File

@ -0,0 +1 @@
# Collection of Common Data Structs

View File

@ -220,3 +220,55 @@ This makes it harder to tell what names are used in scope and where a name in us
This is often used when testing to bring everything under test into the tests module
This is also used as part of the prelude pattern
## Modules In External Files
This works for both binary and library crates
leave only the mod definition in the crate root
```rust
mod example_mod;
```
then place the code that you want inside another file called example_mod.rs
```rust
pub mod inner_example_mod {
pub fn example_fn () {
}
}
```
you only need to to load a file using a ``mod`` declaration once in your module tree
The compiler will know the file is part of the project because of the mod declaration
mod is no an "include" operation like in C/C++ or python
you can then separate inner_example_mod into its own file called inner_example.rs
```rust
pub fn example_fn () {}
```
### Alternate File Patrhs
So far only use idiomatic file paths but you can also use older style of file paths. The compiler will also look for a module named front_of_house declared in the crate root, the compiler will look for the modules code in:
- src/front_of_house.rs (what we covered)
- src/front_of_house/mod.rs (older style, still supported path)
-
For a module named ``inner_example_mod`` that is a submodule of ``front_of_house``, the compiler will look for the modules code in:
- src/front_of_house/inner_example_mod.rs (what we covered)
- src/front_of_house/hosting/mod.rs (older style, still supported path)
If you mix both styles for the same module then you will get a compiler error
Using a mix of both styles for different modules is allowed but you will end up with many mod.rs files which can be confusing
No matter which style is used it will not change how the use path is setup
My preferred style:
- In the file _src/example_mod.rs_
- In the file _src/example_mod/mod.rs
- Only use if it makes sense
- In the file _src/example_mod/inner_example_mod.rs_