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