Hard
A generic type parameter vs. a &dyn Trait parameter in codegen
add_one is a generic function called with i32, f64, and a custom Meters type. What does the compiler actually emit, and how would that change if add_one took a &dyn Trait object instead?
main.rs
fn add_one<T: std::ops::Add<Output = T> + From<u8>>(x: T) -> T {
x + T::from(1u8)
}
struct Meters(f64);
impl std::ops::Add for Meters {
type Output = Meters;
fn add(self, other: Meters) -> Meters {
Meters(self.0 + other.0)
}
}
impl From<u8> for Meters {
fn from(v: u8) -> Self { Meters(v as f64) }
}
fn main() {
let a = add_one(5i32);
let b = add_one(5.0f64);
let c = add_one(Meters(5.0));
println!("{} {} {}", a, b, c.0);
}29 of 53 in Rust