@@ -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
@@ -735,12 +734,9 @@ of numeric literal patterns can be expressed with `to`. The underscore
735
734
(` _ ` ) is a wildcard pattern that matches everything.
736
735
737
736
If the arm with the wildcard pattern was left off in the above
738
- example, the typechecker would reject it at compile time. ` alt `
739
- constructs must be exhaustive: they must have an arm covering every
740
- possible case. (You may use the ` alt check ` construct to write a
741
- non-exhaustive match, but it's highly undesirable to do so. You may
742
- reason that the missing cases will never occur, but the typechecker
743
- provides you with no assurance that your reasoning is correct.)
737
+ example, running it on a number greater than ten (or negative) would
738
+ cause a run-time failure. When no arm matches, ` alt ` constructs do not
739
+ silently fall through—they blow up instead.
744
740
745
741
A powerful application of pattern matching is * destructuring* , where
746
742
you use the matching to get at the contents of data types. Remember
@@ -2093,9 +2089,9 @@ resource type. Rust has several kinds that can be used as type bounds:
2093
2089
mutable fields nor shared boxes.
2094
2090
2095
2091
> *** Note:*** Rust type kinds are syntactically very similar to
2096
- > [ traits ] ( #traits ) when used as type bounds, and can be
2097
- > conveniently thought of as built-in traits . In the future type
2098
- > 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
2099
2095
> knowledge about.
2100
2096
2101
2097
## Generic functions and argument-passing
@@ -2392,9 +2388,9 @@ This makes it possible to rebind a variable without actually mutating
2392
2388
it, which is mostly useful for destructuring (which can rebind, but
2393
2389
not assign).
2394
2390
2395
- # Traits
2391
+ # Interfaces
2396
2392
2397
- Traits are Rust's take on value polymorphism—the thing that
2393
+ Interfaces are Rust's take on value polymorphism—the thing that
2398
2394
object-oriented languages tend to solve with methods and inheritance.
2399
2395
For example, writing a function that can operate on multiple types of
2400
2396
collections.
@@ -2404,27 +2400,27 @@ collections.
2404
2400
2405
2401
## Declaration
2406
2402
2407
- 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
2408
2404
can be applied to a ` self ` value and a number of arguments, using the
2409
2405
dot notation: ` self.foo(arg1, arg2) ` .
2410
2406
2411
- 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
2412
2408
can be converted to a string, with a single method of the same name:
2413
2409
2414
2410
~~~~
2415
- trait to_str {
2411
+ iface to_str {
2416
2412
fn to_str() -> ~str;
2417
2413
}
2418
2414
~~~~
2419
2415
2420
2416
## Implementation
2421
2417
2422
- 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
2423
2419
is used. This defines implementations of ` to_str ` for the ` int ` and
2424
2420
` ~str ` types.
2425
2421
2426
2422
~~~~
2427
- # trait to_str { fn to_str() -> ~str; }
2423
+ # iface to_str { fn to_str() -> ~str; }
2428
2424
impl of to_str for int {
2429
2425
fn to_str() -> ~str { int::to_str(self, 10u) }
2430
2426
}
@@ -2443,13 +2439,13 @@ method that matches the name, and simply calls that.
2443
2439
2444
2440
Implementations are not globally visible. Resolving a method to an
2445
2441
implementation requires that implementation to be in scope. You can
2446
- import and export implementations using the name of the trait they
2442
+ import and export implementations using the name of the interface they
2447
2443
implement (multiple implementations with the same name can be in scope
2448
2444
without problems). Or you can give them an explicit name if you
2449
2445
prefer, using this syntax:
2450
2446
2451
2447
~~~~
2452
- # trait to_str { fn to_str() -> ~str; }
2448
+ # iface to_str { fn to_str() -> ~str; }
2453
2449
impl nil_to_str of to_str for () {
2454
2450
fn to_str() -> ~str { ~"()" }
2455
2451
}
@@ -2465,7 +2461,7 @@ known at compile time, it is possible to specify 'bounds' for type
2465
2461
parameters.
2466
2462
2467
2463
~~~~
2468
- # trait to_str { fn to_str() -> ~str; }
2464
+ # iface to_str { fn to_str() -> ~str; }
2469
2465
fn comma_sep<T: to_str>(elts: ~[T]) -> ~str {
2470
2466
let mut result = ~"", first = true;
2471
2467
for elts.each |elt| {
@@ -2480,18 +2476,18 @@ fn comma_sep<T: to_str>(elts: ~[T]) -> ~str {
2480
2476
The syntax for this is similar to the syntax for specifying that a
2481
2477
parameter type has to be copyable (which is, in principle, another
2482
2478
kind of bound). By declaring ` T ` as conforming to the ` to_str `
2483
- trait , it becomes possible to call methods from that trait on
2479
+ interface , it becomes possible to call methods from that interface on
2484
2480
values of that type inside the function. It will also cause a
2485
2481
compile-time error when anyone tries to call ` comma_sep ` on an array
2486
2482
whose element type does not have a ` to_str ` implementation in scope.
2487
2483
2488
- ## Polymorphic traits
2484
+ ## Polymorphic interfaces
2489
2485
2490
- Traits may contain type parameters. This defines a trait for
2486
+ Interfaces may contain type parameters. This defines an interface for
2491
2487
generalized sequence types:
2492
2488
2493
2489
~~~~
2494
- trait seq<T> {
2490
+ iface seq<T> {
2495
2491
fn len() -> uint;
2496
2492
fn iter(fn(T));
2497
2493
}
@@ -2503,26 +2499,26 @@ impl <T> of seq<T> for ~[T] {
2503
2499
}
2504
2500
~~~~
2505
2501
2506
- Note that the implementation has to explicitly declare the type
2507
- parameter that it binds, ` T ` , before using it to specify its trait type. This is
2502
+ Note that the implementation has to explicitly declare the its
2503
+ parameter ` T ` before using it to specify its interface type. This is
2508
2504
needed because it could also, for example, specify an implementation
2509
2505
of ` seq<int> ` —the ` of ` clause * refers* to a type, rather than defining
2510
2506
one.
2511
2507
2512
- 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
2513
2509
method declarations. So, re-declaring the type parameter
2514
- ` 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
2515
2511
the impl -- would be a compile-time error.
2516
2512
2517
- ## The ` self ` type in traits
2513
+ ## The ` self ` type in interfaces
2518
2514
2519
- In a trait , ` self ` is a special type that you can think of as a
2520
- 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
2521
2517
` T ` replaces the ` self ` type parameter with ` T ` . The following
2522
- trait describes types that support an equality operation:
2518
+ interface describes types that support an equality operation:
2523
2519
2524
2520
~~~~
2525
- trait eq {
2521
+ iface eq {
2526
2522
fn equals(&&other: self) -> bool;
2527
2523
}
2528
2524
@@ -2534,15 +2530,15 @@ impl of eq for int {
2534
2530
Notice that ` equals ` takes an ` int ` argument, rather than a ` self ` argument, in
2535
2531
an implementation for type ` int ` .
2536
2532
2537
- ## Casting to an trait type
2533
+ ## Casting to an interface type
2538
2534
2539
2535
The above allows us to define functions that polymorphically act on
2540
- values of * an* unknown type that conforms to a given trait .
2536
+ values of * an* unknown type that conforms to a given interface .
2541
2537
However, consider this function:
2542
2538
2543
2539
~~~~
2544
2540
# type circle = int; type rectangle = int;
2545
- # trait drawable { fn draw(); }
2541
+ # iface drawable { fn draw(); }
2546
2542
# impl of drawable for int { fn draw() {} }
2547
2543
# fn new_circle() -> int { 1 }
2548
2544
fn draw_all<T: drawable>(shapes: ~[T]) {
@@ -2553,14 +2549,14 @@ fn draw_all<T: drawable>(shapes: ~[T]) {
2553
2549
~~~~
2554
2550
2555
2551
You can call that on an array of circles, or an array of squares
2556
- (assuming those have suitable ` drawable ` traits defined), but not
2552
+ (assuming those have suitable ` drawable ` interfaces defined), but not
2557
2553
on an array containing both circles and squares.
2558
2554
2559
- 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
2560
2556
the function to be written simply like this:
2561
2557
2562
2558
~~~~
2563
- # trait drawable { fn draw(); }
2559
+ # iface drawable { fn draw(); }
2564
2560
fn draw_all(shapes: ~[drawable]) {
2565
2561
for shapes.each |shape| { shape.draw(); }
2566
2562
}
@@ -2575,11 +2571,11 @@ is very similar to the 'vtables' used in most object-oriented
2575
2571
languages.
2576
2572
2577
2573
To construct such a value, you use the ` as ` operator to cast a value
2578
- to a trait type:
2574
+ to an interface type:
2579
2575
2580
2576
~~~~
2581
2577
# type circle = int; type rectangle = int;
2582
- # trait drawable { fn draw(); }
2578
+ # iface drawable { fn draw(); }
2583
2579
# impl of drawable for int { fn draw() {} }
2584
2580
# fn new_circle() -> int { 1 }
2585
2581
# fn new_rectangle() -> int { 2 }
@@ -2598,10 +2594,10 @@ Note that the allocation of a box is somewhat more expensive than
2598
2594
simply using a type parameter and passing in the value as-is, and much
2599
2595
more expensive than statically resolved method calls.
2600
2596
2601
- ## Trait -less implementations
2597
+ ## Interface -less implementations
2602
2598
2603
2599
If you only intend to use an implementation for static overloading,
2604
- 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
2605
2601
to leave off the ` of ` clause. However, this is only possible when you
2606
2602
are defining an implementation in the same module as the receiver
2607
2603
type, and the receiver type is a named type (i.e., an enum or a
@@ -2624,10 +2620,9 @@ OpenSSL libraries installed, it should 'just work'.
2624
2620
2625
2621
~~~~ {.xfail-test}
2626
2622
use std;
2627
- import libc::c_uint;
2628
2623
2629
2624
extern mod crypto {
2630
- fn SHA1(src: *u8, sz: c_uint , out: *u8) -> *u8;
2625
+ fn SHA1(src: *u8, sz: uint , out: *u8) -> *u8;
2631
2626
}
2632
2627
2633
2628
fn as_hex(data: ~[u8]) -> ~str {
@@ -2639,7 +2634,7 @@ fn as_hex(data: ~[u8]) -> ~str {
2639
2634
fn sha1(data: ~str) -> ~str unsafe {
2640
2635
let bytes = str::bytes(data);
2641
2636
let hash = crypto::SHA1(vec::unsafe::to_ptr(bytes),
2642
- vec::len(bytes) as c_uint , ptr::null());
2637
+ vec::len(bytes), ptr::null());
2643
2638
ret as_hex(vec::unsafe::from_buf(hash, 20u));
2644
2639
}
2645
2640
@@ -2705,7 +2700,7 @@ return a pointer.
2705
2700
2706
2701
~~~~ {.xfail-test}
2707
2702
# extern mod crypto {
2708
- fn SHA1(src: *u8, sz: libc::c_uint , out: *u8) -> *u8;
2703
+ fn SHA1(src: *u8, sz: uint , out: *u8) -> *u8;
2709
2704
# }
2710
2705
~~~~
2711
2706
@@ -2796,7 +2791,7 @@ This pointer will become invalid as soon as the vector it points into
2796
2791
is cleaned up, so you should be very careful how you use it. In this
2797
2792
case, the local variable ` bytes ` outlives the pointer, so we're good.
2798
2793
2799
- Passing a null pointer as the third argument to ` SHA1 ` makes it use a
2794
+ Passing a null pointer as third argument to ` SHA1 ` causes it to use a
2800
2795
static buffer, and thus save us the effort of allocating memory
2801
2796
ourselves. ` ptr::null ` is a generic function that will return an
2802
2797
unsafe null pointer of the correct type (Rust generics are awesome
@@ -2819,17 +2814,15 @@ microsecond-resolution timer.
2819
2814
2820
2815
~~~~
2821
2816
use std;
2822
- import libc::c_ulonglong;
2823
-
2824
- type timeval = {mut tv_sec: c_ulonglong,
2825
- mut tv_usec: c_ulonglong};
2817
+ type timeval = {mut tv_sec: uint,
2818
+ mut tv_usec: uint};
2826
2819
#[nolink]
2827
- extern mod lib_c {
2820
+ extern mod libc {
2828
2821
fn gettimeofday(tv: *timeval, tz: *()) -> i32;
2829
2822
}
2830
2823
fn unix_time_in_microseconds() -> u64 unsafe {
2831
- let x = {mut tv_sec: 0 as c_ulonglong , mut tv_usec: 0 as c_ulonglong };
2832
- lib_c ::gettimeofday(ptr::addr_of(x), ptr::null());
2824
+ let x = {mut tv_sec: 0u , mut tv_usec: 0u };
2825
+ libc ::gettimeofday(ptr::addr_of(x), ptr::null());
2833
2826
ret (x.tv_sec as u64) * 1000_000_u64 + (x.tv_usec as u64);
2834
2827
}
2835
2828
@@ -2845,8 +2838,8 @@ define a record type with the same contents, and declare
2845
2838
2846
2839
The second argument to ` gettimeofday ` (the time zone) is not used by
2847
2840
this program, so it simply declares it to be a pointer to the nil
2848
- type. Since all null pointers have the same representation regardless of
2849
- their referent type , this is safe.
2841
+ type. Since null pointer look the same, no matter which type they are
2842
+ supposed to point at , this is safe.
2850
2843
2851
2844
# Tasks
2852
2845
0 commit comments