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