DEV Community

Discussion on: Blog post: First Kata: "Multiply"

Collapse
 
jeikabu profile image
jeikabu

Not sure if this is in the spirit of code kata, but it was something I've meant to look into. To make it work for most types:

fn multiply<T: std::ops::Mul>(lhs: T, rhs: T) -> T::Output {
    lhs * rhs
}

fn main() {
    assert_eq!(multiply(2u32, 3u32), 6);
    assert_eq!(multiply(2u16, 3u16), 6);
    assert_eq!(multiply(std::u8::MAX, 1), std::u8::MAX);
}
Collapse
 
jonasbn profile image
Jonas Brømsø

Well it is not exactly fight club, so I guess we can talk about everything.

I was thinking about something along the lines of what you suggest and I am on the process of working on a write up of another example, based on another small prototype. I just can not find the time to get the post written.

Your example really got me thinking about the whole type-hype, coming from Perl it very different from what I am used to.

Thanks for the example and suggestion, it gave me some perspective and some Rust code to read and reflect on.