You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-08-08 08:22:52 +02:00
Apply suggestions from code review
Co-authored-by: Dmitri Gribenko <gribozavr@gmail.com>
This commit is contained in:
@ -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
|
||||
|
@ -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,
|
||||
|
Reference in New Issue
Block a user