Q:

How to add constraints on generics

I'm trying to implement a OneToMany relation in Rust.  This is implemented as a couple of HashMaps.
I'm having trouble finding out how I constrain the generic types of the OneToMany struct.  It seem like O and M both need to implement the core::cmp::Eq and collections::hash::Hash traits.  I've been unable to find the required syntax in the docs.
struct OneToMany<O,M> { 
  manyByOne: HashMap<O,Vec<M>>,
  oneByMany: HashMap<M,O>
}

impl<O,M> OneToMany<O,M> {
  fn new(one: O, many: M) -> OneToMany<O,M> {
    OneToMany {
      manyByOne: HashMap::new(),
      oneByMany: HashMap::new(),
    }
  }
}

The compiler errors are:
$ cargo build
   Compiling chat v0.1.0 (file:///home/chris/rust/chat)
src/chat.rs:45:18: 45:30 error: the trait `core::cmp::Eq` is not implemented for the type `O`
src/chat.rs:45       manyByOne: HashMap::new(),
                                ^~~~~~~~~~~~
src/chat.rs:45:18: 45:30 note: the trait `core::cmp::Eq` must be implemented because it is required by `std::collections::hashmap::map::HashMap<K, V, RandomSipHasher>::new`
src/chat.rs:45       manyByOne: HashMap::new(),
                                ^~~~~~~~~~~~
src/chat.rs:45:18: 45:30 error: the trait `collections::hash::Hash` is not implemented for the type `O`
src/chat.rs:45       manyByOne: HashMap::new(),
                                ^~~~~~~~~~~~
src/chat.rs:45:18: 45:30 note: the trait `collections::hash::Hash` must be implemented because it is required by `std::collections::hashmap::map::HashMap<K, V, RandomSipHasher>::new`
src/chat.rs:45       manyByOne: HashMap::new(),
                                ^~~~~~~~~~~~
src/chat.rs:46:18: 46:30 error: the trait `core::cmp::Eq` is not implemented for the type `M`
src/chat.rs:46       oneByMany: HashMap::new(),
                                ^~~~~~~~~~~~
src/chat.rs:46:18: 46:30 note: the trait `core::cmp::Eq` must be implemented because it is required by `std::collections::hashmap::map::HashMap<K, V, RandomSipHasher>::new`
src/chat.rs:46       oneByMany: HashMap::new(),
                                ^~~~~~~~~~~~
src/chat.rs:46:18: 46:30 error: the trait `collections::hash::Hash` is not implemented for the type `M`
src/chat.rs:46       oneByMany: HashMap::new(),
                                ^~~~~~~~~~~~
src/chat.rs:46:18: 46:30 note: the trait `collections::hash::Hash` must be implemented because it is required by `std::collections::hashmap::map::HashMap<K, V, RandomSipHasher>::new`
src/chat.rs:46       oneByMany: HashMap::new(),
                                ^~~~~~~~~~~~
error: aborting due to 4 previous errors
Could not compile `chat`.

Where can I add constraints on O and M?

A:

You can add the constraints on the impl:
impl<O: Eq + Hash, M: Eq + Hash> OneToMany<O,M> 
or, with the new "where" syntax 
impl<O, M> OneToMany<O,M> where O: Eq + Hash, M: Eq + Hash
You can refer to the Guide's section on Traits for some more context on constraints.

