Skip to content

Commit 1f0f7d6

Browse files
committed
Rollup merge of #30273 - Xmasreturns:patch-1, r=steveklabnik
Changes to readability and some clarifications for beginners
2 parents 7fc9a66 + 88c407c commit 1f0f7d6

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/doc/book/traits.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
A trait is a language feature that tells the Rust compiler about
44
functionality a type must provide.
55

6-
Do you remember the `impl` keyword, used to call a function with [method
7-
syntax][methodsyntax]?
6+
Recall the `impl` keyword, used to call a function with [method
7+
syntax][methodsyntax]:
88

99
```rust
1010
struct Circle {
@@ -22,8 +22,8 @@ impl Circle {
2222

2323
[methodsyntax]: method-syntax.html
2424

25-
Traits are similar, except that we define a trait with just the method
26-
signature, then implement the trait for that struct. Like this:
25+
Traits are similar, except that we first define a trait with a method
26+
signature, then implement the trait for a type. In this example, we implement the trait `HasArea` for `Circle`:
2727

2828
```rust
2929
struct Circle {
@@ -399,15 +399,13 @@ fn inverse<T>() -> T
399399
```
400400

401401
This shows off the additional feature of `where` clauses: they allow bounds
402-
where the left-hand side is an arbitrary type (`i32` in this case), not just a
403-
plain type parameter (like `T`). In this example, `i32` must implement
402+
on the left-hand side not only of type parameters `T`, but also of types (`i32` in this case). In this example, `i32` must implement
404403
`ConvertTo<T>`. Rather than defining what `i32` is (since that's obvious), the
405-
`where` clause here is a constraint on `T`.
404+
`where` clause here constrains `T`.
406405

407406
# Default methods
408407

409-
If you already know how a typical implementor will define a method, you can
410-
let your trait supply a default:
408+
A default method can be added to a trait definition if it is already known how a typical implementor will define a method. For example, `is_invalid()` is defined as the opposite of `is_valid()`:
411409

412410
```rust
413411
trait Foo {
@@ -417,9 +415,7 @@ trait Foo {
417415
}
418416
```
419417

420-
Implementors of the `Foo` trait need to implement `is_valid()`, but they don’t
421-
need to implement `is_invalid()`. They’ll get this default behavior. They can
422-
override the default if they so choose:
418+
Implementors of the `Foo` trait need to implement `is_valid()` but not `is_invalid()` due to the added default behavior. This default behavior can still be overridden as in:
423419

424420
```rust
425421
# trait Foo {
@@ -446,7 +442,7 @@ impl Foo for OverrideDefault {
446442

447443
fn is_invalid(&self) -> bool {
448444
println!("Called OverrideDefault.is_invalid!");
449-
true // this implementation is a self-contradiction!
445+
true // overrides the expected value of is_invalid()
450446
}
451447
}
452448

@@ -499,7 +495,7 @@ error: the trait `main::Foo` is not implemented for the type `main::Baz` [E0277]
499495

500496
# Deriving
501497

502-
Implementing traits like `Debug` and `Default` over and over again can become
498+
Implementing traits like `Debug` and `Default` repeatedly can become
503499
quite tedious. For that reason, Rust provides an [attribute][attributes] that
504500
allows you to let Rust automatically implement traits for you:
505501

0 commit comments

Comments
 (0)