You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/doc/book/traits.md
+10-14Lines changed: 10 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -3,8 +3,8 @@
3
3
A trait is a language feature that tells the Rust compiler about
4
4
functionality a type must provide.
5
5
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]:
8
8
9
9
```rust
10
10
structCircle {
@@ -22,8 +22,8 @@ impl Circle {
22
22
23
23
[methodsyntax]: method-syntax.html
24
24
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 struct. Like this:
27
27
28
28
```rust
29
29
structCircle {
@@ -399,15 +399,13 @@ fn inverse<T>() -> T
399
399
```
400
400
401
401
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
404
403
`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`.
406
405
407
406
# Default methods
408
407
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()`:
411
409
412
410
```rust
413
411
traitFoo {
@@ -417,9 +415,7 @@ trait Foo {
417
415
}
418
416
```
419
417
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:
423
419
424
420
```rust
425
421
# traitFoo {
@@ -446,7 +442,7 @@ impl Foo for OverrideDefault {
446
442
447
443
fnis_invalid(&self) ->bool {
448
444
println!("Called OverrideDefault.is_invalid!");
449
-
true//this implementation is a self-contradiction!
445
+
true//overrides the expected value of is_invalid()
450
446
}
451
447
}
452
448
@@ -499,7 +495,7 @@ error: the trait `main::Foo` is not implemented for the type `main::Baz` [E0277]
499
495
500
496
# Deriving
501
497
502
-
Implementing traits like `Debug` and `Default`over and over again can become
498
+
Implementing traits like `Debug` and `Default`repeatedly can become
503
499
quite tedious. For that reason, Rust provides an [attribute][attributes] that
504
500
allows you to let Rust automatically implement traits for you:
0 commit comments