Is there any way to create a type alias for multiple traits?

PartialOrd and Display are traits. It has been discussed how to implement an alias but it was decided that it wasn’t needed.

Instead, you can create a new trait with the traits you want as super traits and provide a blanket implementation:

use std::fmt::Display;

trait PartialDisplay: PartialOrd + Display {}
impl<T: PartialOrd + Display> PartialDisplay for T {}

fn print_min<T: PartialDisplay>(a: &T, b: &T) {
    println!("min = {}", if a < b { a } else { b });
}

fn main() {
    print_min(&45, &46);
    print_min(&"aa", &"bb");
}

Leave a Comment