Skip to content

Commit 191ea3f

Browse files
committed
---
yaml --- r: 22015 b: refs/heads/snap-stage3 c: 9902135 h: refs/heads/master i: 22013: aa4b997 22011: fef3a52 22007: 0380cc4 21999: 8fb21a3 21983: 4fdac0e 21951: 1e3c60b 21887: ba28000 21759: 96ba8da 21503: a413077 v: v3
1 parent 827d102 commit 191ea3f

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,7 +1,7 @@
11
---
22
refs/heads/master: e430a699f2c60890d9b86069fd0c68a70ece7120
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: dd9b6c97b908d31643daddc7bc375069898e7d06
4+
refs/heads/snap-stage3: 9902135bdd5c5ca3c3f04c39a4057c0d0244fc33
55
refs/heads/try: ffbe0e0e00374358b789b0037bcb3a577cd218be
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/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)