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