@@ -1712,6 +1712,29 @@ method declarations. So, re-declaring the type parameter
1712
1712
` T ` as an explicit type parameter for ` len ` -- in either the trait or
1713
1713
the impl -- would be a compile-time error.
1714
1714
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
+
1715
1738
## Bounded type parameters and static method dispatch
1716
1739
1717
1740
Traits give us a language for talking about the abstract capabilities
@@ -1753,7 +1776,7 @@ the preferred way to use traits polymorphically.
1753
1776
1754
1777
This usage of traits is similar to Haskell type classes.
1755
1778
1756
- ## Casting to a trait type and dynamic dispatch
1779
+ ## Casting to a trait type and dynamic method dispatch
1757
1780
1758
1781
The above allows us to define functions that polymorphically act on
1759
1782
values of a single unknown type that conforms to a given trait.
@@ -1836,31 +1859,6 @@ method to call.
1836
1859
1837
1860
This usage of traits is similar to Java interfaces.
1838
1861
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
-
1864
1862
# Modules and crates
1865
1863
1866
1864
The Rust namespace is divided into modules. Each source file starts
0 commit comments