Skip to content

Commit 622caa2

Browse files
author
Nick Hamann
committed
Improve the Traits section of the reference.
1 parent afe2c1f commit 622caa2

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/doc/reference.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,7 +1308,7 @@ type of the value is not required to ascribe to `Sync`.
13081308
### Traits
13091309

13101310
Traits are Rust's take on interfaces. A _trait_ describes a collection of
1311-
methods and associated items (like associated types or associated constants).
1311+
methods and associated items (like associated functions, types, or constants).
13121312

13131313
An example is the following trait `Shape`:
13141314

@@ -1323,14 +1323,15 @@ trait Shape {
13231323

13241324
This defines a trait with two methods, `draw` and `bounding_box`. These
13251325
methods can be invoked on any value whose type has an
1326-
[implementation](#implementations) of this trait. The [method call
1326+
[implementation](#implementations) of this trait, provided that the trait is in
1327+
scope (via a `use` declaration if necessary). The [method call
13271328
syntax](#method-call-expressions) looks like this: `value.bounding_box()`.
13281329

1329-
Trait methods can be defined to take a [`self` type](#self-types) as a
1330-
parameter. In the example above, each method takes `&self` as its first
1331-
parameter. This is sometimes called the _receiver_ of the method, and indicates
1332-
that for any type `T` implementing `Shape`, invoking one of the `Shape` methods
1333-
will immutably borrow the `T` value for the duration of the method call.
1330+
Trait methods take a [`self` type](#self-types) as a parameter. In the example
1331+
above, each method takes `&self` as its first parameter. This is sometimes
1332+
called the _receiver_ of the method, and indicates that for any type `T`
1333+
that implements `Shape`, invoking one of the `Shape` methods will immutably
1334+
borrow the `T` value for the duration of the method call.
13341335

13351336
Traits can include default implementations of methods, as in:
13361337

@@ -1346,9 +1347,9 @@ Here the `baz` method has a default implementation, so types that implement
13461347
`Foo` need only implement `bar`. It is also possible for implementing types
13471348
to override a method that has a default implementation.
13481349

1349-
It is not required that trait methods use a `self` type as a parameter. Methods
1350-
that lack `self` types are called _static methods_. A common use for static
1351-
methods is defining constructors, as the following example illustrates:
1350+
Functions in a trait that do not take a `self` type as a parameter are called
1351+
_associated functions_. A common use for associated functions is defining
1352+
constructors, as the following example illustrates:
13521353

13531354
```
13541355
trait Num {

0 commit comments

Comments
 (0)