Hacker News new | past | comments | ask | show | jobs | submit login

>>the whole language is verbose: compare these 10 lines (https://github.com/andreaferretti/kmeans/blob/935b8966d4fe0d...) with this single line (https://github.com/andreaferretti/kmeans/blob/master/nim/alg...)

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).



The last time this post appeared, I actually submitted a PR which did this, among other things: https://github.com/andreaferretti/kmeans/pull/3

The post wasn't really updated.


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


Oh hey! My bad. I didn't expect you to, really, I know that I don't. And when the parent mentioned it, I made an assumption. Sorry :/


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.


A simple example for addition would be adding two u64s and getting a bignum type.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: