Skip to content

Commit f91216b

Browse files
committed
Rollup merge of #24752 - doomrobo:patch-1, r=steveklabnik
Updated sample code to updated syntax (now compiles). Also tweaked the text to reflect the change.
2 parents 76dd691 + 352838e commit f91216b

File tree

1 file changed

+8
-17
lines changed

1 file changed

+8
-17
lines changed

src/doc/reference.md

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,40 +1102,31 @@ signature. Each type parameter must be explicitly declared, in an
11021102
angle-bracket-enclosed, comma-separated list following the function name.
11031103

11041104
```{.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); }
11071107
}
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 {
11091109
let mut acc = vec![];
1110-
for elt in seq.iter() { acc.push(f(elt)); }
1110+
for elt in seq { acc.push(f(*elt)); }
11111111
acc
11121112
}
11131113
```
11141114

11151115
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.
11171119

11181120
When a generic function is referenced, its type is instantiated based on the
11191121
context of the reference. For example, calling the `iter` function defined
11201122
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)`.
11221124

11231125
The type parameters can also be explicitly supplied in a trailing
11241126
[path](#paths) component after the function name. This might be necessary if
11251127
there is not sufficient context to determine the type parameters. For example,
11261128
`mem::size_of::<u32>() == 4`.
11271129

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-
11391130
#### Unsafety
11401131

11411132
Unsafe operations are those that potentially violate the memory-safety

0 commit comments

Comments
 (0)