Skip to content

Commit c2fd88c

Browse files
committed
---
yaml --- r: 6416 b: refs/heads/master c: 12f6e86 h: refs/heads/master v: v3
1 parent 5a81a2c commit c2fd88c

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 02574a5bdb7900fd6b40bf4f3c93080eafa35d4e
2+
refs/heads/master: 12f6e868f7c52a1329b91e16143c0879ffe747e4

trunk/doc/tutorial/generic.md

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ programs that just can't be typed.
5757

5858
let n = none;
5959

60-
If you never do anything else with none, the compiler will not be able
60+
If you never do anything else with `n`, the compiler will not be able
6161
to assign a type to it. (The same goes for `[]`, in fact.) If you
6262
really want to have such a statement, you'll have to write it like
6363
this:
6464

6565
let n = none::<int>;
66+
// or
67+
let n2: option::t<int>: none;
6668

6769
Note that, in a value expression, `<` already has a meaning as a
6870
comparison operator, so you'll have to write `::<T>` to explicitly
@@ -75,12 +77,44 @@ There are two built-in operations that, perhaps surprisingly, act on
7577
values of any type. It was already mentioned earlier that `log` can
7678
take any type of value and output it as a string.
7779

78-
More interesting is that Rust also defines an ordering for all
79-
datatypes, and allows you to meaningfully apply comparison operators
80-
(`<`, `>`, `<=`, `>=`, `==`, `!=`) to them. For structural types, the
81-
comparison happens left to right, so `"abc" < "bac"` (but note that
82-
`"bac" < "ác"`, because the ordering acts on UTF-8 sequences without
83-
any sophistication).
80+
More interesting is that Rust also defines an ordering for values of
81+
all datatypes, and allows you to meaningfully apply comparison
82+
operators (`<`, `>`, `<=`, `>=`, `==`, `!=`) to them. For structural
83+
types, the comparison happens left to right, so `"abc" < "bac"` (but
84+
note that `"bac" < "ác"`, because the ordering acts on UTF-8 sequences
85+
without any sophistication).
86+
87+
## Kinds
88+
89+
Perhaps surprisingly, the 'copy' (duplicate) operation is not defined
90+
for all Rust types. Resource types (types with destructors) can not be
91+
copied, and neither can any type whose copying would require copying a
92+
resource (such as records or unique boxes containing a resource).
93+
94+
This complicates handling of generic functions. If you have a type
95+
parameter `T`, can you copy values of that type? In Rust, you can't,
96+
unless you explicitly declare that type parameter to have copyable
97+
'kind'. A kind is a type of type.
98+
99+
// This does not compile
100+
fn head_bad<T>(v: [T]) -> T { v[0] }
101+
// This does
102+
fn head<copy T>(v: [T]) -> T { v[0] }
103+
104+
When instantiating a generic function, you can only instantiate it
105+
with types that fit its kinds. So you could not apply `head` to a
106+
resource type.
107+
108+
Rust has three kinds: 'noncopyable', 'copyable', and 'sendable'. By
109+
default, type parameters are considered to be noncopyable. You can
110+
annotate them with the `copy` keyword to declare them copyable, and
111+
with the `send` keyword to make them sendable.
112+
113+
Sendable types are a subset of copyable types. They are types that do
114+
not contain shared (reference counted) types, which are thus uniquely
115+
owned by the function that owns them, and can be sent over channels to
116+
other tasks. Most of the generic functions in the `std::comm` module
117+
take sendable types.
84118

85119
## Generic functions and argument-passing
86120

@@ -102,6 +136,3 @@ pass to a generic higher-order function as being passed by pointer:
102136

103137
NOTE: This is inconvenient, and we are hoping to get rid of this
104138
restriction in the future.
105-
106-
FIXME discuss kinds, when they have settled
107-

trunk/doc/tutorial/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Rust language tutorial
22

3-
<div style="font-weight: bold; color: #a00;">Dev snapshot. Not yet suitable for public consumption.</div>
3+
*(Not quite finished yet. Proceed with caution.)*

0 commit comments

Comments
 (0)