From 22b6a5dc2acb4fd83fa3d90d1a04128c252aedd2 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Mon, 15 Jun 2015 16:06:01 -0400 Subject: [PATCH] Significantly simplify generic example Fixes #26320 --- src/doc/reference.md | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/doc/reference.md b/src/doc/reference.md index c19aec78de2e4..0222635358523 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -928,21 +928,20 @@ A _generic function_ allows one or more _parameterized types_ to appear in its signature. Each type parameter must be explicitly declared, in an angle-bracket-enclosed, comma-separated list following the function name. -```{.ignore} -fn iter(seq: &[T], f: F) where T: Copy, F: Fn(T) { - for elt in seq { f(*elt); } -} -fn map(seq: &[T], f: F) -> Vec where T: Copy, U: Copy, F: Fn(T) -> U { - let mut acc = vec![]; - for elt in seq { acc.push(f(*elt)); } - acc -} +```rust,ignore +// foo is generic over A and B + +fn foo(x: A, y: B) { ``` Inside the function signature and body, the name of the type parameter can be 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. +specified using the `where` syntax: + +```rust,ignore +fn foo(x: T) where T: Debug { +``` When a generic function is referenced, its type is instantiated based on the context of the reference. For example, calling the `iter` function defined