diff --git a/src/idiomatic/leveraging-the-type-system/newtype-pattern/parse-don-t-validate.md b/src/idiomatic/leveraging-the-type-system/newtype-pattern/parse-don-t-validate.md index ae503e2b..a1312e2b 100644 --- a/src/idiomatic/leveraging-the-type-system/newtype-pattern/parse-don-t-validate.md +++ b/src/idiomatic/leveraging-the-type-system/newtype-pattern/parse-don-t-validate.md @@ -35,7 +35,8 @@ impl Username { - The newtype pattern, combined with Rust's module and visibility system, can be used to _guarantee_ that instances of a given type satisfy a set of - invariants.\ + invariants. + In the example above, the raw `String` stored inside the `Username` struct can't be accessed directly from other modules or crates, since it's not marked as `pub` or `pub(in ...)`. Consumers of the `Username` type are forced to use @@ -43,7 +44,7 @@ impl Username { ensuring that all instances of `Username` satisfy those checks. - The `as_str` method allows consumers to access the raw string representation - (e.g. to store it in a database) but, thanks to Rust's borrow checker, they + (e.g., to store it in a database) but, thanks to Rust's borrow checker, they can't modify it. - Stress the importance of evaluating _the entire API surface_ exposed by a diff --git a/src/idiomatic/leveraging-the-type-system/newtype-pattern/semantic-confusion.md b/src/idiomatic/leveraging-the-type-system/newtype-pattern/semantic-confusion.md index 648257b6..20d59897 100644 --- a/src/idiomatic/leveraging-the-type-system/newtype-pattern/semantic-confusion.md +++ b/src/idiomatic/leveraging-the-type-system/newtype-pattern/semantic-confusion.md @@ -4,8 +4,7 @@ minutes: 5 # Semantic Confusion -There is room for confusion whenever a function takes multiple arguments of the -same type: +When a function takes multiple arguments of the same type, call sites are unclear: ```rust # struct LoginError; @@ -21,7 +20,7 @@ pub fn login(username: &str, password: &str) -> Result<(), LoginError> { login(password, username); ``` -The newtype pattern can be used to prevent this class of errors at compile time: +The newtype pattern can prevent this class of errors at compile time: ```rust pub struct Username(String); @@ -50,7 +49,7 @@ login(password, username); - Nonetheless, note that there are legitimate scenarios where a function may take multiple arguments of the same type. In those scenarios, if correctness - is of paramount important, consider using a struct with named fields as input: + is of paramount importance, consider using a struct with named fields as input: ```rust pub struct LoginArguments { pub username: &str,