diff options
| author | Manish Goregaokar <manishsmail@gmail.com> | 2015-04-24 09:49:44 +0530 |
|---|---|---|
| committer | Manish Goregaokar <manishsmail@gmail.com> | 2015-04-24 09:49:44 +0530 |
| commit | f91216bdc2ed8646b1b8a625dadea06a8d29af00 (patch) | |
| tree | a65105333dded533368c4b0ad92eb17a1b796b75 /src/doc/reference.md | |
| parent | 76dd69183f8dc9d65188f9fea177963173a03718 (diff) | |
| parent | 352838ed349d01ae16408221bc6a327bf3651ef2 (diff) | |
| download | rust-f91216bdc2ed8646b1b8a625dadea06a8d29af00.tar.gz rust-f91216bdc2ed8646b1b8a625dadea06a8d29af00.zip | |
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.
Diffstat (limited to 'src/doc/reference.md')
| -rw-r--r-- | src/doc/reference.md | 25 |
1 files changed, 8 insertions, 17 deletions
diff --git a/src/doc/reference.md b/src/doc/reference.md index 67d5b33c972..d150075a0f4 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -1102,40 +1102,31 @@ signature. Each type parameter must be explicitly declared, in an angle-bracket-enclosed, comma-separated list following the function name. ```{.ignore} -fn iter<T>(seq: &[T], f: |T|) { - for elt in seq.iter() { f(elt); } +fn iter<T, F>(seq: &[T], f: F) where T: Copy, F: Fn(T) { + for elt in seq { f(*elt); } } -fn map<T, U>(seq: &[T], f: |T| -> U) -> Vec<U> { +fn map<T, U, F>(seq: &[T], f: F) -> Vec<U> where T: Copy, U: Copy, F: Fn(T) -> U { let mut acc = vec![]; - for elt in seq.iter() { acc.push(f(elt)); } + for elt in seq { acc.push(f(*elt)); } acc } ``` Inside the function signature and body, the name of the type parameter can be -used as a type name. +used as a type name. [Trait](#traits) bounds can be specified for type parameters +to allow methods with that trait to be called on values of that type. This is +specified using the `where` syntax, as in the above example. When a generic function is referenced, its type is instantiated based on the context of the reference. For example, calling the `iter` function defined above on `[1, 2]` will instantiate type parameter `T` with `i32`, and require -the closure parameter to have type `fn(i32)`. +the closure parameter to have type `Fn(i32)`. The type parameters can also be explicitly supplied in a trailing [path](#paths) component after the function name. This might be necessary if there is not sufficient context to determine the type parameters. For example, `mem::size_of::<u32>() == 4`. -Since a parameter type is opaque to the generic function, the set of operations -that can be performed on it is limited. Values of parameter type can only be -moved, not copied. - -``` -fn id<T>(x: T) -> T { x } -``` - -Similarly, [trait](#traits) bounds can be specified for type parameters to -allow methods with that trait to be called on values of that type. - #### Unsafety Unsafe operations are those that potentially violate the memory-safety |
