@@ -34,8 +34,9 @@ high-level features include:
34
34
* *** Higher-order functions.*** Rust functions may take closures as
35
35
arguments or return closures as return values. Closures in Rust are
36
36
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_ .
39
40
* *** Parametric polymorphism (generics).*** Functions and types can be
40
41
parameterized over type variables with optional type constraints.
41
42
* *** Type inference.*** Type annotations on local variable
@@ -2089,9 +2090,9 @@ resource type. Rust has several kinds that can be used as type bounds:
2089
2090
mutable fields nor shared boxes.
2090
2091
2091
2092
> *** 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
2095
2096
> knowledge about.
2096
2097
2097
2098
## Generic functions and argument-passing
@@ -2388,9 +2389,9 @@ This makes it possible to rebind a variable without actually mutating
2388
2389
it, which is mostly useful for destructuring (which can rebind, but
2389
2390
not assign).
2390
2391
2391
- # Interfaces
2392
+ # Traits
2392
2393
2393
- Interfaces are Rust's take on value polymorphism—the thing that
2394
+ Traits are Rust's take on value polymorphism—the thing that
2394
2395
object-oriented languages tend to solve with methods and inheritance.
2395
2396
For example, writing a function that can operate on multiple types of
2396
2397
collections.
@@ -2400,27 +2401,27 @@ collections.
2400
2401
2401
2402
## Declaration
2402
2403
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
2404
2405
can be applied to a ` self ` value and a number of arguments, using the
2405
2406
dot notation: ` self.foo(arg1, arg2) ` .
2406
2407
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
2408
2409
can be converted to a string, with a single method of the same name:
2409
2410
2410
2411
~~~~
2411
- iface to_str {
2412
+ trait to_str {
2412
2413
fn to_str() -> ~str;
2413
2414
}
2414
2415
~~~~
2415
2416
2416
2417
## Implementation
2417
2418
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
2419
2420
is used. This defines implementations of ` to_str ` for the ` int ` and
2420
2421
` ~str ` types.
2421
2422
2422
2423
~~~~
2423
- # iface to_str { fn to_str() -> ~str; }
2424
+ # trait to_str { fn to_str() -> ~str; }
2424
2425
impl of to_str for int {
2425
2426
fn to_str() -> ~str { int::to_str(self, 10u) }
2426
2427
}
@@ -2439,13 +2440,13 @@ method that matches the name, and simply calls that.
2439
2440
2440
2441
Implementations are not globally visible. Resolving a method to an
2441
2442
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
2443
2444
implement (multiple implementations with the same name can be in scope
2444
2445
without problems). Or you can give them an explicit name if you
2445
2446
prefer, using this syntax:
2446
2447
2447
2448
~~~~
2448
- # iface to_str { fn to_str() -> ~str; }
2449
+ # trait to_str { fn to_str() -> ~str; }
2449
2450
impl nil_to_str of to_str for () {
2450
2451
fn to_str() -> ~str { ~"()" }
2451
2452
}
@@ -2461,7 +2462,7 @@ known at compile time, it is possible to specify 'bounds' for type
2461
2462
parameters.
2462
2463
2463
2464
~~~~
2464
- # iface to_str { fn to_str() -> ~str; }
2465
+ # trait to_str { fn to_str() -> ~str; }
2465
2466
fn comma_sep<T: to_str>(elts: ~[T]) -> ~str {
2466
2467
let mut result = ~"", first = true;
2467
2468
for elts.each |elt| {
@@ -2476,18 +2477,18 @@ fn comma_sep<T: to_str>(elts: ~[T]) -> ~str {
2476
2477
The syntax for this is similar to the syntax for specifying that a
2477
2478
parameter type has to be copyable (which is, in principle, another
2478
2479
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
2480
2481
values of that type inside the function. It will also cause a
2481
2482
compile-time error when anyone tries to call ` comma_sep ` on an array
2482
2483
whose element type does not have a ` to_str ` implementation in scope.
2483
2484
2484
- ## Polymorphic interfaces
2485
+ ## Polymorphic traits
2485
2486
2486
- Interfaces may contain type parameters. This defines an interface for
2487
+ Traits may contain type parameters. This defines a trait for
2487
2488
generalized sequence types:
2488
2489
2489
2490
~~~~
2490
- iface seq<T> {
2491
+ trait seq<T> {
2491
2492
fn len() -> uint;
2492
2493
fn iter(fn(T));
2493
2494
}
@@ -2500,25 +2501,25 @@ impl <T> of seq<T> for ~[T] {
2500
2501
~~~~
2501
2502
2502
2503
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
2504
2505
needed because it could also, for example, specify an implementation
2505
2506
of ` seq<int> ` —the ` of ` clause * refers* to a type, rather than defining
2506
2507
one.
2507
2508
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
2509
2510
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
2511
2512
the impl -- would be a compile-time error.
2512
2513
2513
- ## The ` self ` type in interfaces
2514
+ ## The ` self ` type in traits
2514
2515
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
2517
2518
` 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:
2519
2520
2520
2521
~~~~
2521
- iface eq {
2522
+ trait eq {
2522
2523
fn equals(&&other: self) -> bool;
2523
2524
}
2524
2525
@@ -2530,15 +2531,15 @@ impl of eq for int {
2530
2531
Notice that ` equals ` takes an ` int ` argument, rather than a ` self ` argument, in
2531
2532
an implementation for type ` int ` .
2532
2533
2533
- ## Casting to an interface type
2534
+ ## Casting to an trait type
2534
2535
2535
2536
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 .
2537
2538
However, consider this function:
2538
2539
2539
2540
~~~~
2540
2541
# type circle = int; type rectangle = int;
2541
- # iface drawable { fn draw(); }
2542
+ # trait drawable { fn draw(); }
2542
2543
# impl of drawable for int { fn draw() {} }
2543
2544
# fn new_circle() -> int { 1 }
2544
2545
fn draw_all<T: drawable>(shapes: ~[T]) {
@@ -2549,14 +2550,14 @@ fn draw_all<T: drawable>(shapes: ~[T]) {
2549
2550
~~~~
2550
2551
2551
2552
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
2553
2554
on an array containing both circles and squares.
2554
2555
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
2556
2557
the function to be written simply like this:
2557
2558
2558
2559
~~~~
2559
- # iface drawable { fn draw(); }
2560
+ # trait drawable { fn draw(); }
2560
2561
fn draw_all(shapes: ~[drawable]) {
2561
2562
for shapes.each |shape| { shape.draw(); }
2562
2563
}
@@ -2571,11 +2572,11 @@ is very similar to the 'vtables' used in most object-oriented
2571
2572
languages.
2572
2573
2573
2574
To construct such a value, you use the ` as ` operator to cast a value
2574
- to an interface type:
2575
+ to a trait type:
2575
2576
2576
2577
~~~~
2577
2578
# type circle = int; type rectangle = int;
2578
- # iface drawable { fn draw(); }
2579
+ # trait drawable { fn draw(); }
2579
2580
# impl of drawable for int { fn draw() {} }
2580
2581
# fn new_circle() -> int { 1 }
2581
2582
# fn new_rectangle() -> int { 2 }
@@ -2594,10 +2595,10 @@ Note that the allocation of a box is somewhat more expensive than
2594
2595
simply using a type parameter and passing in the value as-is, and much
2595
2596
more expensive than statically resolved method calls.
2596
2597
2597
- ## Interface -less implementations
2598
+ ## Trait -less implementations
2598
2599
2599
2600
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
2601
2602
to leave off the ` of ` clause. However, this is only possible when you
2602
2603
are defining an implementation in the same module as the receiver
2603
2604
type, and the receiver type is a named type (i.e., an enum or a
0 commit comments