Skip to content

Commit 42ab33e

Browse files
committed
Use explicit self in rest of tutorial
/cc: rust-lang#4217
1 parent 171e6a1 commit 42ab33e

File tree

1 file changed

+45
-44
lines changed

1 file changed

+45
-44
lines changed

doc/tutorial.md

+45-44
Original file line numberDiff line numberDiff line change
@@ -2033,7 +2033,7 @@ console, with a single method:
20332033

20342034
~~~~
20352035
trait Printable {
2036-
fn print();
2036+
fn print(&self);
20372037
}
20382038
~~~~
20392039

@@ -2045,13 +2045,13 @@ and `~str`.
20452045
[impls]: #functions-and-methods
20462046

20472047
~~~~
2048-
# trait Printable { fn print(); }
2048+
# trait Printable { fn print(&self); }
20492049
impl int: Printable {
2050-
fn print() { io::println(fmt!("%d", self)) }
2050+
fn print(&self) { io::println(fmt!("%d", *self)) }
20512051
}
20522052
20532053
impl &str: Printable {
2054-
fn print() { io::println(self) }
2054+
fn print(&self) { io::println(*self) }
20552055
}
20562056
20572057
# 1.print();
@@ -2065,14 +2065,14 @@ types might look like the following:
20652065

20662066
~~~~
20672067
trait Seq<T> {
2068-
fn len() -> uint;
2069-
fn iter(b: fn(v: &T));
2068+
fn len(&self) -> uint;
2069+
fn iter(&self, b: fn(v: &T));
20702070
}
20712071
20722072
impl<T> ~[T]: Seq<T> {
2073-
fn len() -> uint { vec::len(self) }
2074-
fn iter(b: fn(v: &T)) {
2075-
for vec::each(self) |elt| { b(elt); }
2073+
fn len(&self) -> uint { vec::len(*self) }
2074+
fn iter(&self, b: fn(v: &T)) {
2075+
for vec::each(*self) |elt| { b(elt); }
20762076
}
20772077
}
20782078
~~~~
@@ -2096,21 +2096,22 @@ trait, `self` is a type, and in an impl, `self` is a value. The
20962096
following trait describes types that support an equality operation:
20972097

20982098
~~~~
2099-
// In a trait, `self` refers to the type implementing the trait
2099+
// In a trait, `self` refers both to the self argument
2100+
// and to the type implementing the trait
21002101
trait Eq {
2101-
fn equals(other: &self) -> bool;
2102+
fn equals(&self, other: &self) -> bool;
21022103
}
21032104
2104-
// In an impl, `self` refers to the value of the receiver
2105+
// In an impl, `self` refers just to the value of the receiver
21052106
impl int: Eq {
2106-
fn equals(other: &int) -> bool { *other == self }
2107+
fn equals(&self, other: &int) -> bool { *other == *self }
21072108
}
21082109
~~~~
21092110

2110-
Notice that in the trait definition, `equals` takes a parameter of
2111-
type `self`. In contrast, in the `impl`, `equals` takes a parameter of
2112-
type `int`, and uses `self` as the name of the receiver (analogous to
2113-
the `this` pointer in C++).
2111+
Notice that in the trait definition, `equals` takes a
2112+
second parameter of type `self`.
2113+
In contrast, in the `impl`, `equals` takes a second parameter of
2114+
type `int`, only using `self` as the name of the receiver.
21142115

21152116
## Bounded type parameters and static method dispatch
21162117

@@ -2120,7 +2121,7 @@ define _bounds_ on type parameters, so that we can then operate on
21202121
generic types.
21212122

21222123
~~~~
2123-
# trait Printable { fn print(); }
2124+
# trait Printable { fn print(&self); }
21242125
fn print_all<T: Printable>(printable_things: ~[T]) {
21252126
for printable_things.each |thing| {
21262127
thing.print();
@@ -2138,7 +2139,7 @@ Type parameters can have multiple bounds by separating them with spaces,
21382139
as in this version of `print_all` that copies elements.
21392140

21402141
~~~
2141-
# trait Printable { fn print(); }
2142+
# trait Printable { fn print(&self); }
21422143
fn print_all<T: Printable Copy>(printable_things: ~[T]) {
21432144
let mut i = 0;
21442145
while i < printable_things.len() {
@@ -2163,9 +2164,9 @@ However, consider this function:
21632164

21642165
~~~~
21652166
# type Circle = int; type Rectangle = int;
2166-
# impl int: Drawable { fn draw() {} }
2167+
# impl int: Drawable { fn draw(&self) {} }
21672168
# fn new_circle() -> int { 1 }
2168-
trait Drawable { fn draw(); }
2169+
trait Drawable { fn draw(&self); }
21692170
21702171
fn draw_all<T: Drawable>(shapes: ~[T]) {
21712172
for shapes.each |shape| { shape.draw(); }
@@ -2181,7 +2182,7 @@ needed, a trait name can alternately be used as a type, called
21812182
an _object_.
21822183

21832184
~~~~
2184-
# trait Drawable { fn draw(); }
2185+
# trait Drawable { fn draw(&self); }
21852186
fn draw_all(shapes: &[@Drawable]) {
21862187
for shapes.each |shape| { shape.draw(); }
21872188
}
@@ -2194,14 +2195,14 @@ value to an object:
21942195

21952196
~~~~
21962197
# type Circle = int; type Rectangle = bool;
2197-
# trait Drawable { fn draw(); }
2198+
# trait Drawable { fn draw(&self); }
21982199
# fn new_circle() -> Circle { 1 }
21992200
# fn new_rectangle() -> Rectangle { true }
22002201
# fn draw_all(shapes: &[@Drawable]) {}
22012202
2202-
impl Circle: Drawable { fn draw() { ... } }
2203+
impl Circle: Drawable { fn draw(&self) { ... } }
22032204
2204-
impl Rectangle: Drawable { fn draw() { ... } }
2205+
impl Rectangle: Drawable { fn draw(&self) { ... } }
22052206
22062207
let c: @Circle = @new_circle();
22072208
let r: @Rectangle = @new_rectangle();
@@ -2218,8 +2219,8 @@ for example, an `@Circle` may not be cast to an `~Drawable`.
22182219

22192220
~~~
22202221
# type Circle = int; type Rectangle = int;
2221-
# trait Drawable { fn draw(); }
2222-
# impl int: Drawable { fn draw() {} }
2222+
# trait Drawable { fn draw(&self); }
2223+
# impl int: Drawable { fn draw(&self) {} }
22232224
# fn new_circle() -> int { 1 }
22242225
# fn new_rectangle() -> int { 2 }
22252226
// A managed object
@@ -2244,7 +2245,7 @@ The `static` keyword distinguishes static methods from methods that have a `self
22442245

22452246
~~~~
22462247
trait Shape {
2247-
fn area() -> float;
2248+
fn area(&self) -> float;
22482249
static fn new_shape(area: float) -> Shape;
22492250
}
22502251
~~~~
@@ -2271,25 +2272,25 @@ Types that implement a trait must also implement its supertraits.
22712272
For example, we can define a `Circle` trait that only types that also have the `Shape` trait can have:
22722273

22732274
~~~~
2274-
trait Shape { fn area() -> float; }
2275-
trait Circle : Shape { fn radius() -> float; }
2275+
trait Shape { fn area(&self) -> float; }
2276+
trait Circle : Shape { fn radius(&self) -> float; }
22762277
~~~~
22772278

22782279
Now, implementations of `Circle` methods can call `Shape` methods:
22792280

22802281
~~~~
2281-
# trait Shape { fn area() -> float; }
2282-
# trait Circle : Shape { fn radius() -> float; }
2282+
# trait Shape { fn area(&self) -> float; }
2283+
# trait Circle : Shape { fn radius(&self) -> float; }
22832284
# struct Point { x: float, y: float }
22842285
# use float::consts::pi;
22852286
# use float::sqrt;
22862287
# fn square(x: float) -> float { x * x }
22872288
struct CircleStruct { center: Point, radius: float }
22882289
impl CircleStruct: Circle {
2289-
fn radius() -> float { sqrt(self.area() / pi) }
2290+
fn radius(&self) -> float { sqrt(self.area() / pi) }
22902291
}
22912292
impl CircleStruct: Shape {
2292-
fn area() -> float { pi * square(self.radius) }
2293+
fn area(&self) -> float { pi * square(self.radius) }
22932294
}
22942295
~~~~
22952296

@@ -2301,8 +2302,8 @@ methods of the supertrait may be called on values of subtrait-bound type paramet
23012302
Refering to the previous example of `trait Circle : Shape`:
23022303

23032304
~~~
2304-
# trait Shape { fn area() -> float; }
2305-
# trait Circle : Shape { fn radius() -> float; }
2305+
# trait Shape { fn area(&self) -> float; }
2306+
# trait Circle : Shape { fn radius(&self) -> float; }
23062307
fn radius_times_area<T: Circle>(c: T) -> float {
23072308
// `c` is both a Circle and a Shape
23082309
c.radius() * c.area()
@@ -2312,10 +2313,10 @@ fn radius_times_area<T: Circle>(c: T) -> float {
23122313
Likewise, supertrait methods may also be called on trait objects.
23132314

23142315
~~~ {.xfail-test}
2315-
# trait Shape { fn area() -> float; }
2316-
# trait Circle : Shape { fn radius() -> float; }
2317-
# impl int: Shape { fn area() -> float { 0.0 } }
2318-
# impl int: Circle { fn radius() -> float { 0.0 } }
2316+
# trait Shape { fn area(&self) -> float; }
2317+
# trait Circle : Shape { fn radius(&self) -> float; }
2318+
# impl int: Shape { fn area(&self) -> float { 0.0 } }
2319+
# impl int: Circle { fn radius(&self) -> float { 0.0 } }
23192320
# let mycircle = 0;
23202321
23212322
let mycircle: Circle = @mycircle as @Circle;
@@ -2385,9 +2386,9 @@ mod farm {
23852386
23862387
// Note - visibility modifiers on impls currently have no effect
23872388
impl Farm {
2388-
priv fn feed_chickens() { ... }
2389-
priv fn feed_cows() { ... }
2390-
fn add_chicken(c: Chicken) { ... }
2389+
priv fn feed_chickens(&self) { ... }
2390+
priv fn feed_cows(&self) { ... }
2391+
fn add_chicken(&self, c: Chicken) { ... }
23912392
}
23922393
23932394
pub fn feed_animals(farm: &Farm) {
@@ -2407,7 +2408,7 @@ fn main() {
24072408
# enum Human = int;
24082409
# fn make_me_a_farm() -> farm::Farm { farm::make_me_a_farm() }
24092410
# fn make_me_a_chicken() -> Chicken { 0 }
2410-
# impl Human { fn rest() { } }
2411+
# impl Human { fn rest(&self) { } }
24112412
~~~
24122413

24132414
## Crates

0 commit comments

Comments
 (0)