Trait Bounds

When working with generics, you often want to require the types to implement some trait, so that you can call this trait’s methods.

You can do this with T: Trait or impl Trait:

  1. fn duplicate<T: Clone>(a: T) -> (T, T) {
  2. (a.clone(), a.clone())
  3. }
  4. // Syntactic sugar for:
  5. // fn add_42_millions<T: Into<i32>>(x: T) -> i32 {
  6. fn add_42_millions(x: impl Into<i32>) -> i32 {
  7. x.into() + 42_000_000
  8. }
  9. // struct NotClonable;
  10. fn main() {
  11. let foo = String::from("foo");
  12. let pair = duplicate(foo);
  13. println!("{pair:?}");
  14. let many = add_42_millions(42_i8);
  15. println!("{many}");
  16. let many_more = add_42_millions(10_000_000);
  17. println!("{many_more}");
  18. }

Show a where clause, students will encounter it when reading code.

fn duplicate<T>(a: T) -> (T, T) where T: Clone, { (a.clone(), a.clone()) }

  • It declutters the function signature if you have many parameters.
  • It has additional features making it more powerful.
    • If someone asks, the extra feature is that the type on the left of “:” can be arbitrary, like Option<T>.