Skip to content

Commit 118b58b

Browse files
committed
---
yaml --- r: 31224 b: refs/heads/dist-snap c: 2370474 h: refs/heads/master v: v3
1 parent b3e2968 commit 118b58b

File tree

2 files changed

+103
-26
lines changed

2 files changed

+103
-26
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
99
refs/heads/incoming: d9317a174e434d4c99fc1a37fd7dc0d2f5328d37
10-
refs/heads/dist-snap: 748f2e09096a324d7f2764bd1d54f094a42ef248
10+
refs/heads/dist-snap: 23704740c2e5ddf4e7c184a25d3eed216dd47b82
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/dist-snap/doc/rust.md

Lines changed: 102 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ There are several kinds of item:
593593
* [type definitions](#type-definitions)
594594
* [enumerations](#enumerations)
595595
* [resources](#resources)
596-
* [interfaces](#interfaces)
596+
* [traits](#traits)
597597
* [implementations](#implementations)
598598

599599
Some items form an implicit scope for the declaration of sub-items. In other
@@ -1012,8 +1012,8 @@ parameter is given a [`copy` bound](#type-kinds).
10121012
fn id<T: copy>(x: T) -> T { x }
10131013
~~~~
10141014

1015-
Similarly, [interface](#interfaces) bounds can be specified for type
1016-
parameters to allow methods of that interface to be called on values
1015+
Similarly, [trait](#traits) bounds can be specified for type
1016+
parameters to allow methods with that trait to be called on values
10171017
of that type.
10181018

10191019
#### Extern functions
@@ -1153,11 +1153,11 @@ class file_descriptor {
11531153
self.fd = fd; self.name = none;
11541154
}
11551155
priv {
1156-
let mut name: option<str>;
1156+
let mut name: option<~str>;
11571157
}
1158-
fn get_name() -> str {
1158+
fn get_name() -> ~str {
11591159
alt self.name {
1160-
none { fail "File has no name!"; }
1160+
none { fail ~"File has no name!"; }
11611161
some(n) { n }
11621162
}
11631163
}
@@ -1215,17 +1215,17 @@ class file<A: copy> {
12151215
Classes do not support inheritance, except through traits. As a
12161216
result, all class method dispatch is static (non-virtual).
12171217

1218-
A class may implement a trait (see [interfaces](#interfaces)):
1218+
A class may implement a trait (see [traits](#traits)):
12191219

12201220
~~~~
12211221
trait to_str {
1222-
fn to_str() -> str;
1222+
fn to_str() -> ~str;
12231223
}
12241224
12251225
class file : to_str {
12261226
let fd: *libc::FILE;
12271227
new(fd: *libc::FILE) { self.fd = fd; }
1228-
fn to_str() -> str { "a file" }
1228+
fn to_str() -> ~str { ~"a file" }
12291229
}
12301230
~~~~
12311231

@@ -1249,9 +1249,9 @@ The order of fields in a class instance is significant; its runtime
12491249
representation is the same as that of a record with identical fields
12501250
laid out in the same order.
12511251

1252-
### Interfaces
1252+
### Traits
12531253

1254-
An _interface item_ describes a set of method types. _[implementation
1254+
A _trait item_ describes a set of method types. _[implementation
12551255
items](#implementations)_ can be used to provide implementations of
12561256
those methods for a specific type.
12571257

@@ -1265,12 +1265,12 @@ iface shape {
12651265
}
12661266
~~~~
12671267

1268-
This defines an interface with two methods. All values which have
1269-
[implementations](#implementations) of this interface in scope can
1268+
This defines a trait with two methods. All values that have
1269+
[implementations](#implementations) of this trait in scope can
12701270
have their `draw` and `bounding_box` methods called, using
12711271
`value.bounding_box()` [syntax](#field-expressions).
12721272

1273-
Type parameters can be specified for an interface to make it generic.
1273+
Type parameters can be specified for a trait to make it generic.
12741274
These appear after the name, using the same syntax used in [generic
12751275
functions](#generic-functions).
12761276

@@ -1282,10 +1282,10 @@ iface seq<T> {
12821282
}
12831283
~~~~
12841284

1285-
Generic functions may use interfaces as bounds on their type
1286-
parameters. This will have two effects: only types that implement the
1287-
interface can be used to instantiate the parameter, and within the
1288-
generic function, the methods of the interface can be called on values
1285+
Generic functions may use traits as bounds on their type
1286+
parameters. This will have two effects: only types that have the trait
1287+
may instantiate the parameter, and within the
1288+
generic function, the methods of the trait can be called on values
12891289
that have the parameter's type. For example:
12901290

12911291
~~~~
@@ -1298,8 +1298,8 @@ fn draw_twice<T: shape>(surface: surface, sh: T) {
12981298
}
12991299
~~~~
13001300

1301-
Interface items also define a type with the same name as the
1302-
interface. Values of this type are created by
1301+
Trait items also define a type with the same name as the
1302+
trait. Values of this type are created by
13031303
[casting](#type-cast-expressions) values (of a type for which an
13041304
implementation of the given interface is in scope) to the interface
13051305
type.
@@ -1321,7 +1321,7 @@ instantiate type parameters that are bounded on their interface.
13211321
### Implementations
13221322

13231323
An _implementation item_ provides an implementation of an
1324-
[interface](#interfaces) for a type.
1324+
[interface](#traits) for a type.
13251325

13261326
~~~~
13271327
# type point = {x: float, y: float};
@@ -1682,7 +1682,7 @@ When the type of the expression to the left of the dot is a boxed
16821682
record, it is automatically derferenced to make the field access
16831683
possible.
16841684

1685-
Field access syntax is overloaded for [interface method](#interfaces)
1685+
Field access syntax is overloaded for [trait method](#traits)
16861686
access. When no matching field is found, or the expression to the left
16871687
of the dot is not a (boxed) record, an
16881688
[implementation](#implementations) that matches this type and the
@@ -2067,11 +2067,10 @@ conditional expression evaluates to `false`, the `while` expression completes.
20672067
An example:
20682068

20692069
~~~~
2070-
# let mut i = 0;
2071-
# let println = io::println;
2070+
let mut i = 0;
20722071
20732072
while i < 10 {
2074-
println(~"hello\n");
2073+
io::println(~"hello\n");
20752074
i = i + 1;
20762075
}
20772076
~~~~
@@ -2758,6 +2757,84 @@ let bo: binop = add;
27582757
x = bo(5,7);
27592758
~~~~~~~~
27602759

2760+
### Trait types
2761+
2762+
Every trait item (see [traits](#traits)) defines a type with the same name
2763+
as the trait. For a trait `T`, cast expressions introduce values of type `T`:
2764+
2765+
~~~~~~~~
2766+
// doc extractor doesn't recognize trait -- fix it
2767+
iface printable {
2768+
fn to_str() -> ~str;
2769+
}
2770+
2771+
impl of printable for ~str {
2772+
fn to_str() -> ~str { self }
2773+
}
2774+
2775+
fn print(a: printable) {
2776+
io::println(a.to_str());
2777+
}
2778+
2779+
fn main() {
2780+
print(~"meow" as printable);
2781+
}
2782+
~~~~~~~~
2783+
2784+
In this example, the trait `printable` occurs as a type in both the type signature of
2785+
`print`, and the cast expression in `main`.
2786+
2787+
### Class types
2788+
2789+
Every class item defines a type. See [classes](#classes).
2790+
2791+
### Type parameters
2792+
2793+
Within the body of an item that has type parameter declarations, the names of its type parameters are types:
2794+
2795+
~~~~~~~
2796+
fn map<A: copy, B: copy>(f: fn(A) -> B, xs: ~[A]) -> ~[B] {
2797+
if xs.len() == 0 { ret ~[]; }
2798+
let first: B = f(xs[0]);
2799+
let rest: ~[B] = map(f, xs.slice(1, xs.len()));
2800+
ret ~[first] + rest;
2801+
}
2802+
~~~~~~~
2803+
2804+
Here, `first` has type `B`, referring to `map`'s `B` type parameter; and `rest` has
2805+
type `~[B]`, a vector type with element type `B`.
2806+
2807+
### Self type
2808+
2809+
The special type `self` has a meaning within methods inside a class or
2810+
impl item. It refers to the type of the implicit `self` argument. For
2811+
example, in:
2812+
2813+
~~~~~~
2814+
iface printable {
2815+
fn to_str() -> ~str;
2816+
}
2817+
2818+
impl of printable for ~str {
2819+
fn to_str() -> ~str { self }
2820+
}
2821+
~~~~~~
2822+
2823+
`self` refers to the value of type `str` that is the receiver for a
2824+
call to the method `to_str`. Similarly, in a class declaration:
2825+
2826+
~~~~~~
2827+
class cat {
2828+
let mut meows: uint;
2829+
new() { self.meows = 0; }
2830+
fn meow() { self.meows = self.meows + 1; }
2831+
}
2832+
~~~~~~
2833+
2834+
`self` refers to the class instance that is the receiver of the method
2835+
(except in the constructor `new`, where `self` is the class instance
2836+
that the constructor implicitly returns).
2837+
27612838
## Type kinds
27622839

27632840
Types in Rust are categorized into three kinds, based on whether they

0 commit comments

Comments
 (0)