Bounds

When working with generics, the type parameters often must use traits as bounds to
stipulate what functionality a type implements. For example, the following
example uses the trait Display to print and so it requires T to be bound
by Display; that is, T must implement Display.

  1. // Define a function `printer` that takes a generic type `T` which
  2. // must implement trait `Display`.
  3. fn printer<T: Display>(t: T) {
  4. println!("{}", t);
  5. }

Bounding restricts the generic to types that conform to the bounds. That is:

  1. struct S<T: Display>(T);
  2. // Error! `Vec<T>` does not implement `Display`. This
  3. // specialization will fail.
  4. let s = S(vec![1]);

Another effect of bounding is that generic instances are allowed to access the
methods of traits specified in the bounds. For example:

  1. // A trait which implements the print marker: `{:?}`.
  2. use std::fmt::Debug;
  3. trait HasArea {
  4. fn area(&self) -> f64;
  5. }
  6. impl HasArea for Rectangle {
  7. fn area(&self) -> f64 { self.length * self.height }
  8. }
  9. #[derive(Debug)]
  10. struct Rectangle { length: f64, height: f64 }
  11. #[allow(dead_code)]
  12. struct Triangle { length: f64, height: f64 }
  13. // The generic `T` must implement `Debug`. Regardless
  14. // of the type, this will work properly.
  15. fn print_debug<T: Debug>(t: &T) {
  16. println!("{:?}", t);
  17. }
  18. // `T` must implement `HasArea`. Any function which meets
  19. // the bound can access `HasArea`'s function `area`.
  20. fn area<T: HasArea>(t: &T) -> f64 { t.area() }
  21. fn main() {
  22. let rectangle = Rectangle { length: 3.0, height: 4.0 };
  23. let _triangle = Triangle { length: 3.0, height: 4.0 };
  24. print_debug(&rectangle);
  25. println!("Area: {}", area(&rectangle));
  26. //print_debug(&_triangle);
  27. //println!("Area: {}", area(&_triangle));
  28. // ^ TODO: Try uncommenting these.
  29. // | Error: Does not implement either `Debug` or `HasArea`.
  30. }

As an additional note, where clauses can also be used to apply bounds in
some cases to be more expressive.

See also:

std::fmt, structs, and traits