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
|
- 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
|
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
|
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
|
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
|
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.
|
ensuring that all instances of `Username` satisfy those checks.
|
||||||
|
|
||||||
- The `as_str` method allows consumers to access the raw string representation
|
- 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.
|
can't modify it.
|
||||||
|
|
||||||
- Stress the importance of evaluating _the entire API surface_ exposed by a
|
- Stress the importance of evaluating _the entire API surface_ exposed by a
|
||||||
|
@ -4,8 +4,7 @@ minutes: 5
|
|||||||
|
|
||||||
# Semantic Confusion
|
# Semantic Confusion
|
||||||
|
|
||||||
There is room for confusion whenever a function takes multiple arguments of the
|
When a function takes multiple arguments of the same type, call sites are unclear:
|
||||||
same type:
|
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
# struct LoginError;
|
# struct LoginError;
|
||||||
@ -21,7 +20,7 @@ pub fn login(username: &str, password: &str) -> Result<(), LoginError> {
|
|||||||
login(password, username);
|
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
|
```rust
|
||||||
pub struct Username(String);
|
pub struct Username(String);
|
||||||
@ -50,7 +49,7 @@ login(password, username);
|
|||||||
|
|
||||||
- Nonetheless, note that there are legitimate scenarios where a function may
|
- Nonetheless, note that there are legitimate scenarios where a function may
|
||||||
take multiple arguments of the same type. In those scenarios, if correctness
|
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
|
```rust
|
||||||
pub struct LoginArguments {
|
pub struct LoginArguments {
|
||||||
pub username: &str,
|
pub username: &str,
|
||||||
|
Reference in New Issue
Block a user