diff --git a/.obsidian/workspace.json b/.obsidian/workspace.json index 617d4ce..63eb70e 100644 --- a/.obsidian/workspace.json +++ b/.obsidian/workspace.json @@ -27,12 +27,12 @@ "state": { "type": "markdown", "state": { - "file": "Project Organization.md", + "file": "Modules and Use.md", "mode": "source", "source": false }, "icon": "lucide-file", - "title": "Project Organization" + "title": "Modules and Use" } }, { @@ -48,20 +48,6 @@ "icon": "lucide-file", "title": "Project Organization" } - }, - { - "id": "bc05904661f131c7", - "type": "leaf", - "state": { - "type": "markdown", - "state": { - "file": "Constants.md", - "mode": "source", - "source": false - }, - "icon": "lucide-file", - "title": "Constants" - } } ], "currentTab": 2 @@ -208,23 +194,24 @@ }, "active": "773e0725828dabb7", "lastOpenFiles": [ - "Paths.md", + "data_types.md", "Project Organization.md", + "does_not_compile.svg", "Modules and Use.md", + "Structures.md", + "Primitives.md", + "Paths.md", "Crates.md", "Untitled.canvas", "Packages.md", "Enums.md", - "Primitives.md", "Packages and Crates.md", "Good and Bad Code/Commenting Pratices", "Good and Bad Code", - "data_types.md", "Data Types.md", "ownership.md", "Variables.md", "README.md", - "Constants.md", - "Structures.md" + "Constants.md" ] } \ No newline at end of file diff --git a/Paths.md b/Paths.md index 31a57b3..f987157 100644 --- a/Paths.md +++ b/Paths.md @@ -64,3 +64,14 @@ can be called by the relative path ``front_of_house::hosting::add_to_waitlist(); the public API that is determined by the developer is the contract with the users through other crates. There are many considerations to your public api to make it easier for people to depend on your crate These details and considerations are more concretely outlined in the [The Rust API Guidelines](https://rust-lang.github.io/api-guidelines/) + +## Starting Relative Paths with super +can contract relative paths starting at the parent rather than the root of the crate + +This is done by using the ``super`` keyword at the start of the path it is equivalent to the ``..`` syntax +useful for accessing items that we know are in the parent module which can make rearranging the module tree easier when the module is closely related to the parent but may be moved in the future + +```rust + super::deliver_order(); +``` +deliver order now can be located else where in the parent module and not forced to be in the child module diff --git a/Project Organization.md b/Project Organization.md index 7ccceb4..c863928 100644 --- a/Project Organization.md +++ b/Project Organization.md @@ -18,3 +18,90 @@ These features are known collectively as the ``module system`` they include - [**Paths**](Paths.md): A way of naming an item, such as a struct, function or module ## Best Practices for Packages with a Binary and a Library +A package can contain both a main.rs binary crate root and a lib.rs library crate root and both crates will have the package name by default + +the binary crate will have just enough to start and executable that calls code within the library allow for other projects to benefit by just using the library part of the package for most of the functionality + +The module tree should be defined in src/lib.rs then any items can be used in the binary crate by starting paths with the package name. + +The binary crate then becomes a user of the library crate, it can only use the public API, which helps you design a good API by being both the author and client + +## Public Structs Enums, Functions and Modules +This can be done by using the ``pub`` keyword +Everything is private by default to other code +### Module +This holds code that can be in a different file +``mod`` keyword +normally defined in the src/lib.rs file where the module tree is stored +ex. +```rust +pub mod Name_of_module { + fn private_function () {} +} + +pub mod Name_of_mod; // should be located in Name_of_mod.rs or in name_of_mod/mod.rs or in parent_mod/name_of_mod/child.rs +``` + +### Function +can be in a impl block or by itself just add ``pub`` keyword in front +Should be in a mod block so that the module can be used +ex. +```rust +pub fn public_function () {} + +pub mod name_of_mod { + pub fn example_pub_function () {} +} +``` + +### Struct +This only makes the use of the struct public but not the data stored inside it each needs to be declared as public same with associated functions +can have its own impl, function inside must be explicitly public in order to call +if any data inside is private then a constructor MUST BE USED +ex. +```rust +pub struct PublicStruct {} + +pub mod name_of_mod { + + pub struct ExampleStruct { + pub x: i8, + y: u16, + } + + impl ExampleStruct { + pub fn getter_example_struct (&self) -> i8 { + x + } + } + } +} + +// --snip-- +// consumption +// this is not VALID y must be pub in order to write to +// this needs a contructor +let use_of_struct = crate::name_of_mod::ExampleStruct { + x: 8, + y: 16, // not valid should be done by a fn that has access +} +println!("{}", use_of_struct.getter_example_struct()); +``` + +if this compiled this would output 8 +### Enum +This does not require the variants be be made public in order to use +variants are pub by default if enum is +ex. +```rust +pub mod name_of_mod { + pub enum ExampleEnum { + Variant1(i8), + Variant2(i16), + } +} + +// --snip-- +// consumption +let enum1 = crate::name_of_mod::Variant1(8); +``` \ No newline at end of file diff --git a/data_types.md b/data_types.md index a1fe3da..78f006d 100644 --- a/data_types.md +++ b/data_types.md @@ -513,12 +513,13 @@ fn main() { } ``` output +``` [src/main.rs:10:16] 30 * scale = 60 [src/main.rs:14:5] &rect1 = Rectangle { width: 60, height: 50, } - +``` ## Methods Fucntions that are more closely related to structs