Skip to content

Commit 77af425

Browse files
committed
---
yaml --- r: 30255 b: refs/heads/incoming c: ce4e09b h: refs/heads/master i: 30253: b50f6d7 30251: 00e92a2 30247: 1d46ca3 30239: e5cba6b v: v3
1 parent da706b0 commit 77af425

File tree

2 files changed

+15
-75
lines changed

2 files changed

+15
-75
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ refs/heads/try: d324a424d8f84b1eb049b12cf34182bda91b0024
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: d0c6ce338884ee21843f4b40bf6bf18d222ce5df
9-
refs/heads/incoming: 4fc164a549fc342401d9d935c0e663af43d88e4b
9+
refs/heads/incoming: ce4e09b70951de252a75dc959afc302ca6fd0421
1010
refs/heads/dist-snap: 2f32a1581f522e524009138b33b1c7049ced668d
1111
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1212
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/incoming/doc/tutorial.md

Lines changed: 14 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,14 +1551,13 @@ fn contains(v: ~[int], elt: int) -> bool {
15511551

15521552
## Generic functions
15531553

1554-
Throughout this tutorial, we've been defining functions like
1555-
that act only on single data types. It is 2012, and we no longer
1556-
expect to be defining such functions again and again for every type
1557-
they apply to. Thus, Rust allows functions and datatypes to have type
1558-
parameters.
1554+
Throughout this tutorial, we've been defining functions that act only on
1555+
single data types. It's a burden to define such functions again and again for
1556+
every type they apply to. Thus, Rust allows functions and datatypes to have
1557+
type parameters.
15591558

15601559
~~~~
1561-
fn map<T, U>(vector: ~[T], function: fn(T) -> U) -> ~[U] {
1560+
fn map<T, U>(vector: &[T], function: fn(T) -> U) -> ~[U] {
15621561
let mut accumulator = ~[];
15631562
for vector.each |element| {
15641563
vec::push(accumulator, function(element));
@@ -1577,51 +1576,20 @@ inside them, but you can pass them around.
15771576

15781577
## Generic datatypes
15791578

1580-
Generic `type` and `enum` declarations follow the same pattern:
1581-
1582-
~~~~
1583-
type circular_buf<T> = {start: uint,
1584-
end: uint,
1585-
buf: ~[mut T]};
1586-
1587-
enum option<T> { some(T), none }
1588-
~~~~
1589-
1590-
You can then declare a function to take a `circular_buf<u8>` or return
1591-
an `option<~str>`, or even an `option<T>` if the function itself is
1592-
generic.
1593-
1594-
The `option` type given above exists in the core library and is the
1595-
way Rust programs express the thing that in C would be a nullable
1596-
pointer. The nice part is that you have to explicitly unpack an
1597-
`option` type, so accidental null pointer dereferences become
1598-
impossible.
1599-
1600-
## Type-inference and generics
1601-
1602-
Rust's type inferrer works very well with generics, but there are
1603-
programs that just can't be typed.
1579+
Generic `type`, `struct`, and `enum` declarations follow the same pattern:
16041580

16051581
~~~~
1606-
let n = option::None;
1607-
# option::iter(n, fn&(&&x:int) {})
1608-
~~~~
1609-
1610-
If you never do anything else with `n`, the compiler will not be able
1611-
to assign a type to it. (The same goes for `[]`, the empty vector.) If
1612-
you really want to have such a statement, you'll have to write it like
1613-
this:
1582+
struct Stack<T> {
1583+
elements: ~[mut T]
1584+
}
16141585
1615-
~~~~
1616-
let n2: Option<int> = option::None;
1617-
// or
1618-
let n = option::None::<int>;
1586+
enum Maybe<T> {
1587+
Just(T),
1588+
Nothing
1589+
}
16191590
~~~~
16201591

1621-
Note that, in a value expression, `<` already has a meaning as a
1622-
comparison operator, so you'll have to write `::<T>` to explicitly
1623-
give a type to a name that denotes a generic value. Fortunately, this
1624-
is rarely necessary.
1592+
These declarations produce valid types like `Stack<u8>` and `Maybe<int>`.
16251593

16261594
## Kinds
16271595

@@ -1661,34 +1629,6 @@ resource type. Rust has several kinds that can be used as type bounds:
16611629
> kinds will actually be traits that the compiler has special
16621630
> knowledge about.
16631631
1664-
## Generic functions and argument-passing
1665-
1666-
The previous section mentioned that arguments are passed by pointer or
1667-
by value based on their type. There is one situation in which this is
1668-
difficult. If you try this program:
1669-
1670-
~~~~{.xfail-test}
1671-
fn plus1(x: int) -> int { x + 1 }
1672-
vec::map(~[1, 2, 3], plus1);
1673-
~~~~
1674-
1675-
You will get an error message about argument passing styles
1676-
disagreeing. The reason is that generic types are always passed by
1677-
reference, so `map` expects a function that takes its argument by
1678-
reference. The `plus1` you defined, however, uses the default,
1679-
efficient way to pass integers, which is by value. To get around this
1680-
issue, you have to explicitly mark the arguments to a function that
1681-
you want to pass to a generic higher-order function as being passed by
1682-
pointer, using the `&&` sigil:
1683-
1684-
~~~~
1685-
fn plus1(&&x: int) -> int { x + 1 }
1686-
vec::map(~[1, 2, 3], plus1);
1687-
~~~~
1688-
1689-
> ***Note:*** This is inconvenient, and we are hoping to get rid of
1690-
> this restriction in the future.
1691-
16921632
# Modules and crates
16931633

16941634
The Rust namespace is divided into modules. Each source file starts

0 commit comments

Comments
 (0)