Skip to content

Commit d72d1ee

Browse files
committed
---
yaml --- r: 24123 b: refs/heads/master c: 9902135 h: refs/heads/master i: 24121: a8890ba 24119: d2f6b04 v: v3
1 parent 5e896b1 commit d72d1ee

File tree

2 files changed

+25
-27
lines changed

2 files changed

+25
-27
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: dd9b6c97b908d31643daddc7bc375069898e7d06
2+
refs/heads/master: 9902135bdd5c5ca3c3f04c39a4057c0d0244fc33
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: cd6f24f9d14ac90d167386a56e7a6ac1f0318195
55
refs/heads/try: ffbe0e0e00374358b789b0037bcb3a577cd218be

trunk/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)