Skip to content

Commit 8eb1bab

Browse files
committed
---
yaml --- r: 30723 b: refs/heads/incoming c: 9902135 h: refs/heads/master i: 30721: a6d18b8 30719: 6c92920 v: v3
1 parent 4911e01 commit 8eb1bab

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
@@ -6,7 +6,7 @@ refs/heads/try: d324a424d8f84b1eb049b12cf34182bda91b0024
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
9-
refs/heads/incoming: dd9b6c97b908d31643daddc7bc375069898e7d06
9+
refs/heads/incoming: 9902135bdd5c5ca3c3f04c39a4057c0d0244fc33
1010
refs/heads/dist-snap: 2f32a1581f522e524009138b33b1c7049ced668d
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/incoming/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)