Where clauses

A bound can also be expressed using a where clause immediately
before the opening {, rather than at the type’s first mention.
Additionally, where clauses can apply bounds to arbitrary types,
rather than just to type parameters.

Some cases that a where clause is useful:

  • When specifying generic types and bounds separately is clearer:
  1. impl <A: TraitB + TraitC, D: TraitE + TraitF> MyTrait<A, D> for YourType {}
  2. // Expressing bounds with a `where` clause
  3. impl <A, D> MyTrait<A, D> for YourType where
  4. A: TraitB + TraitC,
  5. D: TraitE + TraitF {}
  • When using a where clause is more expressive than using normal syntax.
    The impl in this example cannot be directly expressed without a where clause:
  1. use std::fmt::Debug;
  2. trait PrintInOption {
  3. fn print_in_option(self);
  4. }
  5. // Because we would otherwise have to express this as `T: Debug` or
  6. // use another method of indirect approach, this requires a `where` clause:
  7. impl<T> PrintInOption for T where
  8. Option<T>: Debug {
  9. // We want `Option<T>: Debug` as our bound because that is what's
  10. // being printed. Doing otherwise would be using the wrong bound.
  11. fn print_in_option(self) {
  12. println!("{:?}", Some(self));
  13. }
  14. }
  15. fn main() {
  16. let vec = vec![1, 2, 3];
  17. vec.print_in_option();
  18. }

See also:

RFC, struct, and trait