Hi all! I've just started learning rust, coming from C++ (and as an extra C#) backgrounds. I'm currently reading the Rust Programming Language book (https://doc.rust-lang.org/book) and I'm at the generics + traits + lifetimes part. I tried to do some own generic programming in rust but I quickly hit a roadblock (probably because I haven't yet read through the whole book). So the problem itself is given a type and its implementation:
struct Rect<T> {Width: T, Height: T }
impl<T> Rect<T> {
pub fn area(&self) -> T { self.Width * self.Height }
}
will not compile because we don't know if T can be even multiplied and if we constraint T to std::ops::Mul as the compiler suggests we don't know whether T * T still returns another T. Now in C++ I would just do a templates with deduced return type:
template <typename T>
concept Mul = requires(T&& t) { t * t; };
template <Mul T>
struct Rect {
T Width; T Height;
auto area() const { return Width * Height; }
};
But that auto return type is of course not a thing in rust. So my question is how would I design a similar generic Rect struct with rust?