Skip to content

Commit 0b3dba4

Browse files
committed
Change iface and interface to trait. Close #2967.
1 parent 7c1339b commit 0b3dba4

File tree

1 file changed

+38
-37
lines changed

1 file changed

+38
-37
lines changed

doc/tutorial.md

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ high-level features include:
3434
* ***Higher-order functions.*** Rust functions may take closures as
3535
arguments or return closures as return values. Closures in Rust are
3636
very powerful and used pervasively.
37-
* ***Interface polymorphism.*** Rust's type system features a unique
38-
combination of Java-style interfaces and Haskell-style typeclasses.
37+
* ***Trait polymorphism.*** Rust's type system features a unique
38+
combination of Java-style interfaces and Haskell-style typeclasses
39+
called _traits_.
3940
* ***Parametric polymorphism (generics).*** Functions and types can be
4041
parameterized over type variables with optional type constraints.
4142
* ***Type inference.*** Type annotations on local variable
@@ -2089,9 +2090,9 @@ resource type. Rust has several kinds that can be used as type bounds:
20892090
mutable fields nor shared boxes.
20902091

20912092
> ***Note:*** Rust type kinds are syntactically very similar to
2092-
> [interfaces](#interfaces) when used as type bounds, and can be
2093-
> conveniently thought of as built-in interfaces. In the future type
2094-
> kinds will actually be interfaces that the compiler has special
2093+
> [traits](#traits) when used as type bounds, and can be
2094+
> conveniently thought of as built-in traits. In the future type
2095+
> kinds will actually be traits that the compiler has special
20952096
> knowledge about.
20962097
20972098
## Generic functions and argument-passing
@@ -2388,9 +2389,9 @@ This makes it possible to rebind a variable without actually mutating
23882389
it, which is mostly useful for destructuring (which can rebind, but
23892390
not assign).
23902391

2391-
# Interfaces
2392+
# Traits
23922393

2393-
Interfaces are Rust's take on value polymorphism—the thing that
2394+
Traits are Rust's take on value polymorphism—the thing that
23942395
object-oriented languages tend to solve with methods and inheritance.
23952396
For example, writing a function that can operate on multiple types of
23962397
collections.
@@ -2400,27 +2401,27 @@ collections.
24002401
24012402
## Declaration
24022403

2403-
An interface consists of a set of methods. A method is a function that
2404+
A trait consists of a set of methods. A method is a function that
24042405
can be applied to a `self` value and a number of arguments, using the
24052406
dot notation: `self.foo(arg1, arg2)`.
24062407

2407-
For example, we could declare the interface `to_str` for things that
2408+
For example, we could declare the trait `to_str` for things that
24082409
can be converted to a string, with a single method of the same name:
24092410

24102411
~~~~
2411-
iface to_str {
2412+
trait to_str {
24122413
fn to_str() -> ~str;
24132414
}
24142415
~~~~
24152416

24162417
## Implementation
24172418

2418-
To actually implement an interface for a given type, the `impl` form
2419+
To actually implement an trait for a given type, the `impl` form
24192420
is used. This defines implementations of `to_str` for the `int` and
24202421
`~str` types.
24212422

24222423
~~~~
2423-
# iface to_str { fn to_str() -> ~str; }
2424+
# trait to_str { fn to_str() -> ~str; }
24242425
impl of to_str for int {
24252426
fn to_str() -> ~str { int::to_str(self, 10u) }
24262427
}
@@ -2439,13 +2440,13 @@ method that matches the name, and simply calls that.
24392440

24402441
Implementations are not globally visible. Resolving a method to an
24412442
implementation requires that implementation to be in scope. You can
2442-
import and export implementations using the name of the interface they
2443+
import and export implementations using the name of the trait they
24432444
implement (multiple implementations with the same name can be in scope
24442445
without problems). Or you can give them an explicit name if you
24452446
prefer, using this syntax:
24462447

24472448
~~~~
2448-
# iface to_str { fn to_str() -> ~str; }
2449+
# trait to_str { fn to_str() -> ~str; }
24492450
impl nil_to_str of to_str for () {
24502451
fn to_str() -> ~str { ~"()" }
24512452
}
@@ -2461,7 +2462,7 @@ known at compile time, it is possible to specify 'bounds' for type
24612462
parameters.
24622463

24632464
~~~~
2464-
# iface to_str { fn to_str() -> ~str; }
2465+
# trait to_str { fn to_str() -> ~str; }
24652466
fn comma_sep<T: to_str>(elts: ~[T]) -> ~str {
24662467
let mut result = ~"", first = true;
24672468
for elts.each |elt| {
@@ -2476,18 +2477,18 @@ fn comma_sep<T: to_str>(elts: ~[T]) -> ~str {
24762477
The syntax for this is similar to the syntax for specifying that a
24772478
parameter type has to be copyable (which is, in principle, another
24782479
kind of bound). By declaring `T` as conforming to the `to_str`
2479-
interface, it becomes possible to call methods from that interface on
2480+
trait, it becomes possible to call methods from that trait on
24802481
values of that type inside the function. It will also cause a
24812482
compile-time error when anyone tries to call `comma_sep` on an array
24822483
whose element type does not have a `to_str` implementation in scope.
24832484

2484-
## Polymorphic interfaces
2485+
## Polymorphic traits
24852486

2486-
Interfaces may contain type parameters. This defines an interface for
2487+
Traits may contain type parameters. This defines a trait for
24872488
generalized sequence types:
24882489

24892490
~~~~
2490-
iface seq<T> {
2491+
trait seq<T> {
24912492
fn len() -> uint;
24922493
fn iter(fn(T));
24932494
}
@@ -2500,25 +2501,25 @@ impl <T> of seq<T> for ~[T] {
25002501
~~~~
25012502

25022503
Note that the implementation has to explicitly declare the its
2503-
parameter `T` before using it to specify its interface type. This is
2504+
parameter `T` before using it to specify its trait type. This is
25042505
needed because it could also, for example, specify an implementation
25052506
of `seq<int>`—the `of` clause *refers* to a type, rather than defining
25062507
one.
25072508

2508-
The type parameters bound by an iface are in scope in each of the
2509+
The type parameters bound by a trait are in scope in each of the
25092510
method declarations. So, re-declaring the type parameter
2510-
`T` as an explicit type parameter for `len` -- in either the iface or
2511+
`T` as an explicit type parameter for `len` -- in either the trait or
25112512
the impl -- would be a compile-time error.
25122513

2513-
## The `self` type in interfaces
2514+
## The `self` type in traits
25142515

2515-
In an interface, `self` is a special type that you can think of as a
2516-
type parameter. An implementation of the interface for any given type
2516+
In a trait, `self` is a special type that you can think of as a
2517+
type parameter. An implementation of the trait for any given type
25172518
`T` replaces the `self` type parameter with `T`. The following
2518-
interface describes types that support an equality operation:
2519+
trait describes types that support an equality operation:
25192520

25202521
~~~~
2521-
iface eq {
2522+
trait eq {
25222523
fn equals(&&other: self) -> bool;
25232524
}
25242525
@@ -2530,15 +2531,15 @@ impl of eq for int {
25302531
Notice that `equals` takes an `int` argument, rather than a `self` argument, in
25312532
an implementation for type `int`.
25322533

2533-
## Casting to an interface type
2534+
## Casting to an trait type
25342535

25352536
The above allows us to define functions that polymorphically act on
2536-
values of *an* unknown type that conforms to a given interface.
2537+
values of *an* unknown type that conforms to a given trait.
25372538
However, consider this function:
25382539

25392540
~~~~
25402541
# type circle = int; type rectangle = int;
2541-
# iface drawable { fn draw(); }
2542+
# trait drawable { fn draw(); }
25422543
# impl of drawable for int { fn draw() {} }
25432544
# fn new_circle() -> int { 1 }
25442545
fn draw_all<T: drawable>(shapes: ~[T]) {
@@ -2549,14 +2550,14 @@ fn draw_all<T: drawable>(shapes: ~[T]) {
25492550
~~~~
25502551

25512552
You can call that on an array of circles, or an array of squares
2552-
(assuming those have suitable `drawable` interfaces defined), but not
2553+
(assuming those have suitable `drawable` traits defined), but not
25532554
on an array containing both circles and squares.
25542555

2555-
When this is needed, an interface name can be used as a type, causing
2556+
When this is needed, a trait name can be used as a type, causing
25562557
the function to be written simply like this:
25572558

25582559
~~~~
2559-
# iface drawable { fn draw(); }
2560+
# trait drawable { fn draw(); }
25602561
fn draw_all(shapes: ~[drawable]) {
25612562
for shapes.each |shape| { shape.draw(); }
25622563
}
@@ -2571,11 +2572,11 @@ is very similar to the 'vtables' used in most object-oriented
25712572
languages.
25722573

25732574
To construct such a value, you use the `as` operator to cast a value
2574-
to an interface type:
2575+
to a trait type:
25752576

25762577
~~~~
25772578
# type circle = int; type rectangle = int;
2578-
# iface drawable { fn draw(); }
2579+
# trait drawable { fn draw(); }
25792580
# impl of drawable for int { fn draw() {} }
25802581
# fn new_circle() -> int { 1 }
25812582
# fn new_rectangle() -> int { 2 }
@@ -2594,10 +2595,10 @@ Note that the allocation of a box is somewhat more expensive than
25942595
simply using a type parameter and passing in the value as-is, and much
25952596
more expensive than statically resolved method calls.
25962597

2597-
## Interface-less implementations
2598+
## Trait-less implementations
25982599

25992600
If you only intend to use an implementation for static overloading,
2600-
and there is no interface available that it conforms to, you are free
2601+
and there is no trait available that it conforms to, you are free
26012602
to leave off the `of` clause. However, this is only possible when you
26022603
are defining an implementation in the same module as the receiver
26032604
type, and the receiver type is a named type (i.e., an enum or a

0 commit comments

Comments
 (0)