Skip to content

Commit

Permalink
Merge pull request #2371 from sadika9/patch-1
Browse files Browse the repository at this point in the history
Use same naming for Rhs as libcore/ops
  • Loading branch information
steveklabnik authored Jun 14, 2020
2 parents 30cd9df + 1f8e2ec commit fd7e93c
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/ch19-03-advanced-traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,30 +116,30 @@ The default generic type in this code is within the `Add` trait. Here is its
definition:

```rust
trait Add<RHS=Self> {
trait Add<Rhs=Self> {
type Output;

fn add(self, rhs: RHS) -> Self::Output;
fn add(self, rhs: Rhs) -> Self::Output;
}
```

This code should look generally familiar: a trait with one method and an
associated type. The new part is `RHS=Self`: this syntax is called *default
type parameters*. The `RHS` generic type parameter (short for “right hand
associated type. The new part is `Rhs=Self`: this syntax is called *default
type parameters*. The `Rhs` generic type parameter (short for “right hand
side”) defines the type of the `rhs` parameter in the `add` method. If we don’t
specify a concrete type for `RHS` when we implement the `Add` trait, the type
of `RHS` will default to `Self`, which will be the type we’re implementing
specify a concrete type for `Rhs` when we implement the `Add` trait, the type
of `Rhs` will default to `Self`, which will be the type we’re implementing
`Add` on.

When we implemented `Add` for `Point`, we used the default for `RHS` because we
When we implemented `Add` for `Point`, we used the default for `Rhs` because we
wanted to add two `Point` instances. Let’s look at an example of implementing
the `Add` trait where we want to customize the `RHS` type rather than using the
the `Add` trait where we want to customize the `Rhs` type rather than using the
default.

We have two structs, `Millimeters` and `Meters`, holding values in different
units. We want to add values in millimeters to values in meters and have the
implementation of `Add` do the conversion correctly. We can implement `Add` for
`Millimeters` with `Meters` as the `RHS`, as shown in Listing 19-15.
`Millimeters` with `Meters` as the `Rhs`, as shown in Listing 19-15.

<span class="filename">Filename: src/lib.rs</span>

Expand All @@ -151,7 +151,7 @@ implementation of `Add` do the conversion correctly. We can implement `Add` for
`Millimeters` to add `Millimeters` to `Meters`</span>

To add `Millimeters` and `Meters`, we specify `impl Add<Meters>` to set the
value of the `RHS` type parameter instead of using the default of `Self`.
value of the `Rhs` type parameter instead of using the default of `Self`.

You’ll use default type parameters in two main ways:

Expand Down

0 comments on commit fd7e93c

Please sign in to comment.