The Nim code's more concise in this case but the Rust code can be more clearly written:
impl Add for Point {
type Output = Point;
fn add(self, other: Point) -> Point {
Point(self.0 + other.0, self.1 + other.1)
}
}
The "type Output = Point" line isn't boilerplate, it makes it possible to define the result of an addition as something other than a Point. (Off of the top of my head I can't think of any use cases, but I'm happy with the capability personally).
Hi, author of the post here. I did in fact, update the post, mentioning your PR in the first paragraph.
I cannot really change the rest of the content: I am reporting the mail I sent, and that was it. Changing it after the fact would only add more confusion
For addition I can't either, but you could use it to overload multiplication of two (mathematical) vectors to a dot-product
impl Mul for Vec2{
type Output = double;
fn mul(self, other: Vec2) -> Double {
self.0*other.0+self.1*other.1)
}
}
Although I must admit I'm not (yet) sure why you need to explicitly state the output and define it for the function. But I have only just started with Rust.
> Although I must admit I'm not (yet) sure why you need to explicitly state the output and define it for the function. But I have only just started with Rust.
I generally assume it's because it can't infer the associated type from function signature, for now.
The Nim code's more concise in this case but the Rust code can be more clearly written:
The "type Output = Point" line isn't boilerplate, it makes it possible to define the result of an addition as something other than a Point. (Off of the top of my head I can't think of any use cases, but I'm happy with the capability personally).