Using traits for bounds on generics

The most widespread use of traits is for writing generic functions or types. For example, the following signature describes a function for consuming any iterator yielding items of type A to produce a collection of A:

fn main() { fn from_iter<T: Iterator<A>>(iterator: T) -> SomeCollection<A> }
fn from_iter<T: Iterator<A>>(iterator: T) -> SomeCollection<A>

Here, the Iterator trait is specifies an interface that a type T must explicitly implement to be used by this generic function.



Favor widespread traits. [FIXME: needs RFC]

Generic types are a form of abstraction, which entails a mental indirection: if a function takes an argument of type T bounded by Trait, clients must first think about the concrete types that implement Trait to understand how and when the function is callable.

To keep the cost of abstraction low, favor widely-known traits. Whenever possible, implement and use traits provided as part of the standard library. Do not introduce new traits for generics lightly; wait until there are a wide range of types that can implement the type.