Skip to content

Commit 4c0bf11

Browse files
committed
---
yaml --- r: 31444 b: refs/heads/dist-snap c: 4463172 h: refs/heads/master v: v3
1 parent 013b5db commit 4c0bf11

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+881
-577
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
99
refs/heads/incoming: d9317a174e434d4c99fc1a37fd7dc0d2f5328d37
10-
refs/heads/dist-snap: f1f9066274114637510a48ab1087750262ae0699
10+
refs/heads/dist-snap: 44631722ff234241cb7100103320a402fe4fdfa9
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/dist-snap/doc/tutorial.md

Lines changed: 59 additions & 52 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
@@ -734,9 +735,12 @@ of numeric literal patterns can be expressed with `to`. The underscore
734735
(`_`) is a wildcard pattern that matches everything.
735736

736737
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.)
740744

741745
A powerful application of pattern matching is *destructuring*, where
742746
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:
20892093
mutable fields nor shared boxes.
20902094

20912095
> ***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
20952099
> knowledge about.
20962100
20972101
## Generic functions and argument-passing
@@ -2388,9 +2392,9 @@ This makes it possible to rebind a variable without actually mutating
23882392
it, which is mostly useful for destructuring (which can rebind, but
23892393
not assign).
23902394

2391-
# Interfaces
2395+
# Traits
23922396

2393-
Interfaces are Rust's take on value polymorphism—the thing that
2397+
Traits are Rust's take on value polymorphism—the thing that
23942398
object-oriented languages tend to solve with methods and inheritance.
23952399
For example, writing a function that can operate on multiple types of
23962400
collections.
@@ -2400,27 +2404,27 @@ collections.
24002404
24012405
## Declaration
24022406

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
24042408
can be applied to a `self` value and a number of arguments, using the
24052409
dot notation: `self.foo(arg1, arg2)`.
24062410

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
24082412
can be converted to a string, with a single method of the same name:
24092413

24102414
~~~~
2411-
iface to_str {
2415+
trait to_str {
24122416
fn to_str() -> ~str;
24132417
}
24142418
~~~~
24152419

24162420
## Implementation
24172421

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
24192423
is used. This defines implementations of `to_str` for the `int` and
24202424
`~str` types.
24212425

24222426
~~~~
2423-
# iface to_str { fn to_str() -> ~str; }
2427+
# trait to_str { fn to_str() -> ~str; }
24242428
impl of to_str for int {
24252429
fn to_str() -> ~str { int::to_str(self, 10u) }
24262430
}
@@ -2439,13 +2443,13 @@ method that matches the name, and simply calls that.
24392443

24402444
Implementations are not globally visible. Resolving a method to an
24412445
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
24432447
implement (multiple implementations with the same name can be in scope
24442448
without problems). Or you can give them an explicit name if you
24452449
prefer, using this syntax:
24462450

24472451
~~~~
2448-
# iface to_str { fn to_str() -> ~str; }
2452+
# trait to_str { fn to_str() -> ~str; }
24492453
impl nil_to_str of to_str for () {
24502454
fn to_str() -> ~str { ~"()" }
24512455
}
@@ -2461,7 +2465,7 @@ known at compile time, it is possible to specify 'bounds' for type
24612465
parameters.
24622466

24632467
~~~~
2464-
# iface to_str { fn to_str() -> ~str; }
2468+
# trait to_str { fn to_str() -> ~str; }
24652469
fn comma_sep<T: to_str>(elts: ~[T]) -> ~str {
24662470
let mut result = ~"", first = true;
24672471
for elts.each |elt| {
@@ -2476,18 +2480,18 @@ fn comma_sep<T: to_str>(elts: ~[T]) -> ~str {
24762480
The syntax for this is similar to the syntax for specifying that a
24772481
parameter type has to be copyable (which is, in principle, another
24782482
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
24802484
values of that type inside the function. It will also cause a
24812485
compile-time error when anyone tries to call `comma_sep` on an array
24822486
whose element type does not have a `to_str` implementation in scope.
24832487

2484-
## Polymorphic interfaces
2488+
## Polymorphic traits
24852489

2486-
Interfaces may contain type parameters. This defines an interface for
2490+
Traits may contain type parameters. This defines a trait for
24872491
generalized sequence types:
24882492

24892493
~~~~
2490-
iface seq<T> {
2494+
trait seq<T> {
24912495
fn len() -> uint;
24922496
fn iter(fn(T));
24932497
}
@@ -2499,26 +2503,26 @@ impl <T> of seq<T> for ~[T] {
24992503
}
25002504
~~~~
25012505

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
25042508
needed because it could also, for example, specify an implementation
25052509
of `seq<int>`—the `of` clause *refers* to a type, rather than defining
25062510
one.
25072511

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
25092513
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
25112515
the impl -- would be a compile-time error.
25122516

2513-
## The `self` type in interfaces
2517+
## The `self` type in traits
25142518

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
25172521
`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:
25192523

25202524
~~~~
2521-
iface eq {
2525+
trait eq {
25222526
fn equals(&&other: self) -> bool;
25232527
}
25242528
@@ -2530,15 +2534,15 @@ impl of eq for int {
25302534
Notice that `equals` takes an `int` argument, rather than a `self` argument, in
25312535
an implementation for type `int`.
25322536

2533-
## Casting to an interface type
2537+
## Casting to an trait type
25342538

25352539
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.
25372541
However, consider this function:
25382542

25392543
~~~~
25402544
# type circle = int; type rectangle = int;
2541-
# iface drawable { fn draw(); }
2545+
# trait drawable { fn draw(); }
25422546
# impl of drawable for int { fn draw() {} }
25432547
# fn new_circle() -> int { 1 }
25442548
fn draw_all<T: drawable>(shapes: ~[T]) {
@@ -2549,14 +2553,14 @@ fn draw_all<T: drawable>(shapes: ~[T]) {
25492553
~~~~
25502554

25512555
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
25532557
on an array containing both circles and squares.
25542558

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
25562560
the function to be written simply like this:
25572561

25582562
~~~~
2559-
# iface drawable { fn draw(); }
2563+
# trait drawable { fn draw(); }
25602564
fn draw_all(shapes: ~[drawable]) {
25612565
for shapes.each |shape| { shape.draw(); }
25622566
}
@@ -2571,11 +2575,11 @@ is very similar to the 'vtables' used in most object-oriented
25712575
languages.
25722576

25732577
To construct such a value, you use the `as` operator to cast a value
2574-
to an interface type:
2578+
to a trait type:
25752579

25762580
~~~~
25772581
# type circle = int; type rectangle = int;
2578-
# iface drawable { fn draw(); }
2582+
# trait drawable { fn draw(); }
25792583
# impl of drawable for int { fn draw() {} }
25802584
# fn new_circle() -> int { 1 }
25812585
# fn new_rectangle() -> int { 2 }
@@ -2594,10 +2598,10 @@ Note that the allocation of a box is somewhat more expensive than
25942598
simply using a type parameter and passing in the value as-is, and much
25952599
more expensive than statically resolved method calls.
25962600

2597-
## Interface-less implementations
2601+
## Trait-less implementations
25982602

25992603
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
26012605
to leave off the `of` clause. However, this is only possible when you
26022606
are defining an implementation in the same module as the receiver
26032607
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'.
26202624

26212625
~~~~ {.xfail-test}
26222626
use std;
2627+
import libc::c_uint;
26232628
26242629
extern mod crypto {
2625-
fn SHA1(src: *u8, sz: uint, out: *u8) -> *u8;
2630+
fn SHA1(src: *u8, sz: c_uint, out: *u8) -> *u8;
26262631
}
26272632
26282633
fn as_hex(data: ~[u8]) -> ~str {
@@ -2634,7 +2639,7 @@ fn as_hex(data: ~[u8]) -> ~str {
26342639
fn sha1(data: ~str) -> ~str unsafe {
26352640
let bytes = str::bytes(data);
26362641
let hash = crypto::SHA1(vec::unsafe::to_ptr(bytes),
2637-
vec::len(bytes), ptr::null());
2642+
vec::len(bytes) as c_uint, ptr::null());
26382643
ret as_hex(vec::unsafe::from_buf(hash, 20u));
26392644
}
26402645
@@ -2700,7 +2705,7 @@ return a pointer.
27002705

27012706
~~~~ {.xfail-test}
27022707
# 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;
27042709
# }
27052710
~~~~
27062711

@@ -2791,7 +2796,7 @@ This pointer will become invalid as soon as the vector it points into
27912796
is cleaned up, so you should be very careful how you use it. In this
27922797
case, the local variable `bytes` outlives the pointer, so we're good.
27932798

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
27952800
static buffer, and thus save us the effort of allocating memory
27962801
ourselves. `ptr::null` is a generic function that will return an
27972802
unsafe null pointer of the correct type (Rust generics are awesome
@@ -2814,15 +2819,17 @@ microsecond-resolution timer.
28142819

28152820
~~~~
28162821
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};
28192826
#[nolink]
2820-
extern mod libc {
2827+
extern mod lib_c {
28212828
fn gettimeofday(tv: *timeval, tz: *()) -> i32;
28222829
}
28232830
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());
28262833
ret (x.tv_sec as u64) * 1000_000_u64 + (x.tv_usec as u64);
28272834
}
28282835
@@ -2838,8 +2845,8 @@ define a record type with the same contents, and declare
28382845

28392846
The second argument to `gettimeofday` (the time zone) is not used by
28402847
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.
28432850

28442851
# Tasks
28452852

branches/dist-snap/mk/tests.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ cleantestlibs:
9595
check: cleantestlibs cleantmptestlogs tidy all check-stage2
9696
$(Q)$(S)src/etc/check-summary.py tmp/*.log
9797

98+
check-notidy: cleantestlibs cleantmptestlogs all check-stage2
99+
$(Q)$(S)src/etc/check-summary.py tmp/*.log
100+
98101
check-full: cleantestlibs cleantmptestlogs tidy \
99102
all check-stage1 check-stage2 check-stage3
100103
$(Q)$(S)src/etc/check-summary.py tmp/*.log

0 commit comments

Comments
 (0)