finished ch7.3

This commit is contained in:
darkicewolf50 2025-01-15 11:58:45 -07:00
parent c6299ee523
commit 4c4dd464dd
4 changed files with 108 additions and 22 deletions

View File

@ -27,12 +27,12 @@
"state": { "state": {
"type": "markdown", "type": "markdown",
"state": { "state": {
"file": "Project Organization.md", "file": "Modules and Use.md",
"mode": "source", "mode": "source",
"source": false "source": false
}, },
"icon": "lucide-file", "icon": "lucide-file",
"title": "Project Organization" "title": "Modules and Use"
} }
}, },
{ {
@ -48,20 +48,6 @@
"icon": "lucide-file", "icon": "lucide-file",
"title": "Project Organization" "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 "currentTab": 2
@ -208,23 +194,24 @@
}, },
"active": "773e0725828dabb7", "active": "773e0725828dabb7",
"lastOpenFiles": [ "lastOpenFiles": [
"Paths.md", "data_types.md",
"Project Organization.md", "Project Organization.md",
"does_not_compile.svg",
"Modules and Use.md", "Modules and Use.md",
"Structures.md",
"Primitives.md",
"Paths.md",
"Crates.md", "Crates.md",
"Untitled.canvas", "Untitled.canvas",
"Packages.md", "Packages.md",
"Enums.md", "Enums.md",
"Primitives.md",
"Packages and Crates.md", "Packages and Crates.md",
"Good and Bad Code/Commenting Pratices", "Good and Bad Code/Commenting Pratices",
"Good and Bad Code", "Good and Bad Code",
"data_types.md",
"Data Types.md", "Data Types.md",
"ownership.md", "ownership.md",
"Variables.md", "Variables.md",
"README.md", "README.md",
"Constants.md", "Constants.md"
"Structures.md"
] ]
} }

View File

@ -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. 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 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/) 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

View File

@ -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 - [**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 ## 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.<img style="height:5svh" src="https://doc.rust-lang.org/book/img/ferris/does_not_compile.svg" />
```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());
```
<img style="height:5svh" src="https://doc.rust-lang.org/book/img/ferris/does_not_compile.svg" />
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);
```

View File

@ -513,12 +513,13 @@ fn main() {
} }
``` ```
output output
```
[src/main.rs:10:16] 30 * scale = 60 [src/main.rs:10:16] 30 * scale = 60
[src/main.rs:14:5] &rect1 = Rectangle { [src/main.rs:14:5] &rect1 = Rectangle {
width: 60, width: 60,
height: 50, height: 50,
} }
```
## Methods ## Methods
Fucntions that are more closely related to structs Fucntions that are more closely related to structs