#![feature(specialization)] use std::fmt::*; trait Maybe { fn implements_this(&self) -> bool; fn fmt(&self, &mut Formatter) -> Result; } macro_rules! impl_maybe { ($t:path) => { impl Maybe<$t> for T { default fn implements_this(&self) -> bool { false } default fn fmt(&self, _: &mut Formatter) -> Result { println!("nah"); Ok(()) } } impl Maybe<$t> for T { fn implements_this(&self) -> bool { true } fn fmt(&self, f: &mut Formatter) -> Result { println!("ok"); ::fmt(self, f) } } } } impl_maybe!(Debug); impl_maybe!(Display); impl_maybe!(LowerHex); trait MaybeAll: Maybe + Maybe + Maybe {} impl MaybeAll for T where T: Maybe + Maybe + Maybe {} fn rtformat(s: &str, args: &[&MaybeAll]) { for (c, t) in s.chars().zip(args) { match c { 'd' => println!("{:?}", t) } } } fn main() { rtformat("dDhh", &[&123u8, &"test", &0xffu8, &0xffu8]); }