@@ -1102,40 +1102,31 @@ signature. Each type parameter must be explicitly declared, in an
1102
1102
angle-bracket-enclosed, comma-separated list following the function name.
1103
1103
1104
1104
``` {.ignore}
1105
- fn iter<T>(seq: &[T], f: |T| ) {
1106
- for elt in seq.iter() { f(elt); }
1105
+ fn iter<T, F >(seq: &[T], f: F) where T: Copy, F: Fn(T ) {
1106
+ for elt in seq { f(* elt); }
1107
1107
}
1108
- fn map<T, U>(seq: &[T], f: |T| -> U ) -> Vec<U> {
1108
+ fn map<T, U, F >(seq: &[T], f: F) -> Vec<U> where T: Copy, U: Copy, F: Fn(T ) -> U {
1109
1109
let mut acc = vec![];
1110
- for elt in seq.iter() { acc.push(f(elt)); }
1110
+ for elt in seq { acc.push(f(* elt)); }
1111
1111
acc
1112
1112
}
1113
1113
```
1114
1114
1115
1115
Inside the function signature and body, the name of the type parameter can be
1116
- used as a type name.
1116
+ used as a type name. [ Trait] ( #traits ) bounds can be specified for type parameters
1117
+ to allow methods with that trait to be called on values of that type. This is
1118
+ specified using the ` where ` syntax, as in the above example.
1117
1119
1118
1120
When a generic function is referenced, its type is instantiated based on the
1119
1121
context of the reference. For example, calling the ` iter ` function defined
1120
1122
above on ` [1, 2] ` will instantiate type parameter ` T ` with ` i32 ` , and require
1121
- the closure parameter to have type ` fn (i32)` .
1123
+ the closure parameter to have type ` Fn (i32)` .
1122
1124
1123
1125
The type parameters can also be explicitly supplied in a trailing
1124
1126
[ path] ( #paths ) component after the function name. This might be necessary if
1125
1127
there is not sufficient context to determine the type parameters. For example,
1126
1128
` mem::size_of::<u32>() == 4 ` .
1127
1129
1128
- Since a parameter type is opaque to the generic function, the set of operations
1129
- that can be performed on it is limited. Values of parameter type can only be
1130
- moved, not copied.
1131
-
1132
- ```
1133
- fn id<T>(x: T) -> T { x }
1134
- ```
1135
-
1136
- Similarly, [ trait] ( #traits ) bounds can be specified for type parameters to
1137
- allow methods with that trait to be called on values of that type.
1138
-
1139
1130
#### Unsafety
1140
1131
1141
1132
Unsafe operations are those that potentially violate the memory-safety
0 commit comments