Skip to content

Commit 9902135

Browse files
committed
tutorial: More generics cleanup
1 parent dd9b6c9 commit 9902135

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

doc/tutorial.md

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,29 @@ method declarations. So, re-declaring the type parameter
17121712
`T` as an explicit type parameter for `len` -- in either the trait or
17131713
the impl -- would be a compile-time error.
17141714

1715+
Within a trait definition, `self` is a special type that you can think
1716+
of as a type parameter. An implementation of the trait for any given
1717+
type `T` replaces the `self` type parameter with `T`. Simply, in a
1718+
trait, `self` is a type, and in an impl, `self` is a value. The
1719+
following trait describes types that support an equality operation:
1720+
1721+
~~~~
1722+
// In a trait, `self` refers to the type implementing the trait
1723+
trait Eq {
1724+
fn equals(other: &self) -> bool;
1725+
}
1726+
1727+
// In an impl, self refers to the value of the receiver
1728+
impl int: Eq {
1729+
fn equals(other: &int) -> bool { *other == self }
1730+
}
1731+
~~~~
1732+
1733+
Notice that in the trait definition, `equals` takes a `self` type
1734+
argument, whereas, in the impl, `equals` takes an `int` type argument,
1735+
and uses `self` as the name of the receiver (analogous to the `this` pointer
1736+
in C++).
1737+
17151738
## Bounded type parameters and static method dispatch
17161739

17171740
Traits give us a language for talking about the abstract capabilities
@@ -1753,7 +1776,7 @@ the preferred way to use traits polymorphically.
17531776

17541777
This usage of traits is similar to Haskell type classes.
17551778

1756-
## Casting to a trait type and dynamic dispatch
1779+
## Casting to a trait type and dynamic method dispatch
17571780

17581781
The above allows us to define functions that polymorphically act on
17591782
values of a single unknown type that conforms to a given trait.
@@ -1836,31 +1859,6 @@ method to call.
18361859

18371860
This usage of traits is similar to Java interfaces.
18381861

1839-
## The `self` type
1840-
1841-
In a trait, `self` is a special type that you can think of as a
1842-
type parameter. An implementation of the trait for any given type
1843-
`T` replaces the `self` type parameter with `T`. Simply, in a trait,
1844-
`self` is a type, and in an impl, `self` is a value. The following
1845-
trait describes types that support an equality operation:
1846-
1847-
~~~~
1848-
// In a trait, `self` refers to the type implementing the trait
1849-
trait Eq {
1850-
fn equals(&&other: self) -> bool;
1851-
}
1852-
1853-
// In an impl, self refers to the value of the receiver
1854-
impl int: Eq {
1855-
fn equals(&&other: int) -> bool { other == self }
1856-
}
1857-
~~~~
1858-
1859-
Notice that in the trait definition, `equals` takes a `self` type
1860-
argument, whereas, in the impl, `equals` takes an `int` type argument,
1861-
and uses `self` as the name of the receiver (analogous to the `this` pointer
1862-
in C++).
1863-
18641862
# Modules and crates
18651863

18661864
The Rust namespace is divided into modules. Each source file starts

0 commit comments

Comments
 (0)