Skip to content

Commit 6b4b1ea

Browse files
authored
Idiomatic: Misc. minor tweaks to the type system section (#3165)
A handful of minor fixes to the type systems section of Idiomatic. Mostly just fixing wording or markdown formatting. Also rename the "Parse, Don't Validate" slide to "Enforce Invariants at Construction". The naming of this slide seems odd to me when the example code is validating inputs, not doing any parsing. I think what the slide demonstrates is fine, but makes more sense framed as "enforce invariants at construction time, then you can rely on those invariants later".
1 parent 58f6817 commit 6b4b1ea

7 files changed

Lines changed: 12 additions & 14 deletions

File tree

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ SPDX-License-Identifier: CC-BY-4.0
481481
- [Leveraging the Type System](idiomatic/leveraging-the-type-system.md)
482482
- [Newtype Pattern](idiomatic/leveraging-the-type-system/newtype-pattern.md)
483483
- [Semantic Confusion](idiomatic/leveraging-the-type-system/newtype-pattern/semantic-confusion.md)
484-
- [Parse, Don't Validate](idiomatic/leveraging-the-type-system/newtype-pattern/parse-don-t-validate.md)
484+
- [Enforce Invariants](idiomatic/leveraging-the-type-system/newtype-pattern/parse-don-t-validate.md)
485485
- [Is It Encapsulated?](idiomatic/leveraging-the-type-system/newtype-pattern/is-it-encapsulated.md)
486486
- [RAII](idiomatic/leveraging-the-type-system/raii.md)
487487
- [Drop Skipped](idiomatic/leveraging-the-type-system/raii/drop_skipped.md)

src/idiomatic/leveraging-the-type-system/extension-traits.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ SPDX-License-Identifier: CC-BY-4.0
99

1010
# Extension Traits
1111

12-
It may desirable to **extend** foreign types with new inherent methods. For
13-
example, allow your code to check if a string is a palindrome using
14-
method-calling syntax: `s.is_palindrome()`.
12+
It may desirable to **extend** foreign types with new methods. For example,
13+
allow your code to check if a string is a palindrome using method-calling
14+
syntax: `s.is_palindrome()`.
1515

1616
It might feel natural to reach out for an `impl` block:
1717

src/idiomatic/leveraging-the-type-system/newtype-pattern/parse-don-t-validate.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Copyright 2025 Google LLC
77
SPDX-License-Identifier: CC-BY-4.0
88
-->
99

10-
# Parse, Don't Validate
10+
# Enforce Invariants at Construction
1111

1212
The newtype pattern can be leveraged to enforce _invariants_.
1313

@@ -25,7 +25,6 @@ impl Username {
2525
if username.len() > 32 {
2626
return Err(InvalidUsername::TooLong { len: username.len() })
2727
}
28-
// Other validation checks...
2928
Ok(Self(username))
3029
}
3130

src/idiomatic/leveraging-the-type-system/raii/scope_guard.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ fn main() {
6060
fails, the file will still be cleaned up. This ordering is essential for
6161
correctness.
6262

63-
- The `guard()` creates a `ScopeGuard` instance. It a user-defined value (in
64-
this case, `path`) and the cleanup closure that later receives this value.
63+
- The `guard()` creates a `ScopeGuard` instance. It takes a user-defined value
64+
(in this case, `path`) and the cleanup closure that later receives this value.
6565

6666
- The guard's closure runs on scope exit unless it is _defused_ with
6767
`ScopeGuard::into_inner` (removing the value so the guard does nothing on
@@ -75,8 +75,7 @@ fn main() {
7575

7676
- This pattern is also useful when you don't control the cleanup strategy of the
7777
resource object. In this example, `File::drop()` closes the file but does not
78-
delete it, and we can't change the standard library to delete the file instead
79-
(nor should we, it is not a good idea anyway).
78+
delete it.
8079

8180
- The `scopeguard` crate also supports cleanup strategies via the
8281
[`Strategy`](https://docs.rs/scopeguard/latest/scopeguard/trait.Strategy.html)

src/idiomatic/leveraging-the-type-system/token-types/branded-02-phantomdata.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn main() {
7272
lifetimes have a common shorter lifetime (AKA being subtyped).
7373

7474
- Note: This slide compiles, by the end of this slide it should only compile
75-
when `subtyped_lifetimes` is commented out.
75+
when `try_coerce_lifetimes` is commented out.
7676

7777
- There are two important parts of this code:
7878
- The `impl for<'a>` bound on the closure passed to `lifetime_separator`.

src/idiomatic/leveraging-the-type-system/typestate-pattern.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Copyright 2025 Google LLC
77
SPDX-License-Identifier: CC-BY-4.0
88
-->
99

10-
## Typestate Pattern: Problem
10+
# Typestate Pattern: Problem
1111

1212
How can we ensure that only valid operations are allowed on a value based on its
1313
current state?

src/idiomatic/polymorphism/from-oop-to-rust/why-no-inheritance.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ impl SomeTrait for Data {
5757
interchangeably, without being able to specify a concrete type or if a type is
5858
identical to another.
5959

60-
For operations like equality, comparison this allows for comparison and
61-
equality that throws and error or otherwise panics.
60+
For operations like equality or comparison this allows for comparison and
61+
equality that throws an error or otherwise panics.
6262

6363
- Multiple sources of truth for what makes up a data structure and how it
6464
behaves:

0 commit comments

Comments
 (0)