@@ -57,12 +57,14 @@ programs that just can't be typed.
57
57
58
58
let n = none;
59
59
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
61
61
to assign a type to it. (The same goes for ` [] ` , in fact.) If you
62
62
really want to have such a statement, you'll have to write it like
63
63
this:
64
64
65
65
let n = none::<int>;
66
+ // or
67
+ let n2: option::t<int>: none;
66
68
67
69
Note that, in a value expression, ` < ` already has a meaning as a
68
70
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
75
77
values of any type. It was already mentioned earlier that ` log ` can
76
78
take any type of value and output it as a string.
77
79
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.
84
118
85
119
## Generic functions and argument-passing
86
120
@@ -102,6 +136,3 @@ pass to a generic higher-order function as being passed by pointer:
102
136
103
137
NOTE: This is inconvenient, and we are hoping to get rid of this
104
138
restriction in the future.
105
-
106
- FIXME discuss kinds, when they have settled
107
-
0 commit comments