Common types and functions are modified. However, traits without generics can be used.
This is a common feature. He will be monomorphized.
fn get_length<T: Collection>(collection: &T) -> uint { collection.len() }
This is an equivalent non-generic function. Only one copy of this function will be included in the binary.
fn get_length(collection: &Collection) -> uint { collection.len() }
Please note that we could not create a function that receives a Collection value by value because Collection is a sign and therefore does not have a specific size. In this case, a common function is required.
There are a few things you cannot do with generics and some things you cannot do with property references. Obviously, with trait references, you need a trait. In the case of generics, you cannot have a collection vector in which collections have different types (for example, you could not put Vec<int> and String in this vector), but you can use object references: a Vec<&Collection> may contain &Vec<int> and a &String .
Francis gagné
source share