struct S1 { v: i32, } struct S2 { t: f64, } trait ShowMe { fn show(&self); } impl ShowMe for S1 { fn show(&self) { println!("I am an S1, and my value is {}", self.v) } } impl ShowMe for S2 { fn show(&self) { println!("I am an S2, and my value is {}", self.t) } } impl ShowMe for i64 { fn show(&self) { println!("I am a 64-bit integer, and my value is {}", self) } } fn f(v: &dyn ShowMe) { print!("Invoking show()... "); v.show() } fn main() { let a = S1{v:5}; let b = S2{t:0.1}; let c = 666; let t = 0; let mut v: &dyn ShowMe = if t == 0 { &a } else { &b }; f(v); if t == 0 { v = &c }; f(v); }