Skip to content

Commit e3c75de

Browse files
Updates per chatgpt review
1 parent a9dc19e commit e3c75de

4 files changed

Lines changed: 68 additions & 24 deletions

File tree

courses/rust_essentials/180_modules/02-filesystem_hierarchy.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,11 @@ Mapping Modules to Files
3737
* Rust looks for
3838

3939
* :filename:`cleaner.rs`
40-
41-
* Preferred modern style
42-
4340
* :filename:`cleaner/mod.rs`
4441

45-
* Older style
42+
.. note::
43+
44+
:filename:`cleaner/mod.rs` is legacy style but still common
4645

4746
-------------------------
4847
Directory-based Modules

courses/rust_essentials/180_modules/03-visibility.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ Private by Default
1717
* Child module can see everything in its parent
1818
* Parent can only see :rust:`pub` items in its child
1919

20+
----------------------
21+
Private Code Example
22+
----------------------
23+
2024
.. code:: rust
2125
2226
mod outer {
@@ -37,6 +41,10 @@ Private by Default
3741
3842
:error:`error[E0603]: function 'private' is private`
3943

44+
.. note::
45+
46+
Compiler prevents illegal usage
47+
4048
----------------------------
4149
Struct and Enum Visibility
4250
----------------------------

courses/rust_essentials/180_modules/04-encapsulation.rst

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,25 @@ Encapsulation
66
Why Encapsulate?
77
------------------
88

9-
* Invariants - protect internal state of a data structure
9+
* Protect internal state of a data structure
1010

11-
* Ensure data is always valid
12-
13-
* Can only be changed via approved logic
11+
* Ensure data is always valid via getter/setter API's
1412

1513
* Decoupling - hiding implementation details
1614

17-
* Can change your internal code without breaking client
15+
* Change your code without breaking client
1816

1917
* E.g., swapping an array for a :rust:`Vec`
2018

21-
* API Surface - smaller public API
22-
23-
* Easier to document
24-
* Easier to learn
25-
* Harder to misuse
26-
2719
--------------------------
2820
Encapsulation in Structs
2921
--------------------------
3022

3123
* *Gatekeeper* pattern
3224

33-
* Keep fields private (default behavior)
34-
* Provide a :rust:`pub fn new(...)` constructor
25+
* Keep fields private
3526

36-
* Ensure struct starts in valid state
27+
* Only your code can modify fields
3728

3829
* Provide :rust:`pub` getter and setter methods
3930

@@ -76,6 +67,6 @@ Breaking Encapsulation
7667

7768
.. note::
7869

79-
Encapsulation is not just "hiding" code; it is about **guaranteeing correctness**
70+
Encapsulation hides code to help guarantee correctness
8071

81-
If a field is private, you guarantee no outside code can put an "illegal" value in
72+
Private fields cannot have "illegal" values

courses/rust_essentials/180_modules/05-use_super_self.rst

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,35 @@
22
"use", "super", "self"
33
========================
44

5+
-------------------------
6+
Dealing with Long Paths
7+
-------------------------
8+
9+
.. code:: rust
10+
11+
mod greenhouse {
12+
pub mod shelf {
13+
pub mod cactus {
14+
pub fn water_cactus() {
15+
println!("Watering the cactus");
16+
}
17+
pub fn touch_spine() {
18+
println!("Touching the prickly thing");
19+
}
20+
}
21+
}
22+
}
23+
24+
fn main() {
25+
// This is repetitive and hard to read
26+
greenhouse::shelf::cactus::water_cactus();
27+
greenhouse::shelf::cactus::touch_spine();
28+
greenhouse::shelf::cactus::water_cactus();
29+
greenhouse::shelf::cactus::touch_spine();
30+
}
31+
32+
*Wouldn't it be nice to shorten these paths?*
33+
534
--------------------
635
The "use" Shortcut
736
--------------------
@@ -14,17 +43,34 @@ The "use" Shortcut
1443

1544
* As if it was in your own module
1645

17-
* Use the wildcard "*" to get everything
18-
19-
* :rust:`use std::io::*;` gets all items from :rust:`std::io`
20-
* But it makes name clashes more likely
2146

2247
* Simplify (and reduce clashes) with *renaming*
2348

2449
.. code:: rust
2550
2651
use std::io::Result as IoResult;
2752
53+
-----------------------
54+
"use" with a Wildcard
55+
-----------------------
56+
57+
* Use the wildcard "*" (:dfn:`glob import`) to get everything
58+
59+
* When to use it
60+
61+
* Common in :rust:`mod tests` to test private items easily
62+
* Used in preludes to load essential traits
63+
* Speeds up prototyping
64+
65+
* When to avoid it
66+
67+
* Makes it hard to find things for coder and autocomplete
68+
* Globbed modules use the same name causes compilation errors
69+
70+
.. note::
71+
72+
Use **nested imports** - :rust:`use std::io::(self, Read, Write);`
73+
2874
----------------
2975
Relative Paths
3076
----------------

0 commit comments

Comments
 (0)