diff --git a/.obsidian/app.json b/.obsidian/app.json index e609a07..4d49ab3 100644 --- a/.obsidian/app.json +++ b/.obsidian/app.json @@ -1,3 +1,4 @@ { - "promptDelete": false + "promptDelete": false, + "alwaysUpdateLinks": true } \ No newline at end of file diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index de7934c..602188a 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -41,13 +41,13 @@ "state": { "type": "markdown", "state": { - "file": "Crates.md", + "file": "Modules and Use.md", "mode": "source", "backlinks": false, "source": false }, "icon": "lucide-file", - "title": "Crates" + "title": "Modules and Use" } }, { @@ -209,9 +209,10 @@ }, "active": "773e0725828dabb7", "lastOpenFiles": [ - "Packages.md", - "Crates.md", "Project Organization.md", + "Modules and Use.md", + "Crates.md", + "Packages.md", "Enums.md", "Primitives.md", "Packages and Crates.md", diff --git a/Modules and Use.md b/Modules and Use.md new file mode 100644 index 0000000..71a367f --- /dev/null +++ b/Modules and Use.md @@ -0,0 +1,72 @@ +# Use +``use`` the keyword +This is used to bring paths into scope +``pub`` keyword is used to make items public +``as`` keyword for globs and external packages + +# Modules + +## Cheat Sheet +- **Start from the Root Crate**: compiler starts here to look for code to compile + - Normally src/main.rs for binary crates or src/lib.rs for library crates +- **Declaring Modules**: These are declared in the crate root file + - For example if you had a module called garden + - it would be declared by ``mod garden;`` + - The complier would then look in these places + - Inline, within curly brackets that replace the semicolon following `mod garden` + - In the file _src/garden.rs_ + - In the file _src/garden/mod.rs +- **Declaring submodules**: in any other file you can declare submodules in any other file than the root + - it would be declared by ``mod vegetables;`` + - The complier would then look in these places for the submodule + - Inline, directly following `mod vegetables`, within curly brackets instead of the semicolon + - In the file _src/garden/vegetables.rs_ + - In the file _src/garden/vegetables/mod.rs +- **Paths to code in Modules**: once a module is part of your crate, you can refer to the code in that module form anywhere else in that same crate as long as the privacy rules allow using that part of the code + - Example ``Asparagus`` is a struct/type in the graden vegetables module would be found at ``crate::graden::vegetables::Asparagus`` +- **Private vs. public**: code is private by default to the parent module to make it public. + - To make a module public declare it with ``pub mod`` instead of ``mod``. + - To make items within the public module public as well add ``pub`` before their declaration +- **The ``use`` keyword**: This is used to shorten the full path to just the last ``::`` in the declaration + - example use + - ``crate::garden::vegetables::Asparagus`` + - to now + - ``use crate::garden::vegetables::Asparagus;`` + - now can call this module by just ``Asparagus`` + +Here is how this example's file directory would look +``` +backyard +├── Cargo.lock +├── Cargo.toml +└── src + ├── garden + │ └── vegetables.rs + ├── garden.rs + └── main.rs +``` + +src/main.rs +```rust +use crate::garden::vegetables::Asparagus; + +pub mod garden; + +fn main() { + let plant = Asparagus {}; + println!("I'm growing {plant:?}!"); +} +``` + +src/garden.rs +```rust +pub mod vegetables; +``` + +src/garden/vegetables.rs +```rust +#[derive(Debug)] +pub struct Asparagus {} +``` + +## Grouping Related Code in Modules diff --git a/Project Organization.md b/Project Organization.md index 51cad51..bbea3d0 100644 --- a/Project Organization.md +++ b/Project Organization.md @@ -14,5 +14,5 @@ rust has features for your code's organization, which parts are exposed, which d These features are known collectively as the ``module system`` they include - [**Packages**](Packages.md): A feature of Cargo that allows you to build, test and share crates - [**Crates**](Crates.md): A tree of modules that produces a library or an executable -- **Modules** and **use**: lets you control the organization, scope and privacy of paths +- [**Modules** and **use**](Modules%20and%20Use.md): lets you control the organization, scope and privacy of paths - **Paths**: A way of naming an item, such as a struct, function or module