7 ms·
>>the whole language is verbose: compare these 10 lines (https://github.com/andreaferretti/kmeans/blob/935b8966d4fe0d4854d3d69ec0fbfb4dd69a3fd1/rust/src/point/m
by tmerr 12y ago
>>the whole language is verbose: compare these 10 lines (https://github.com/andreaferretti/kmeans/blob/935b8966d4fe0d4854d3d69ec0fbfb4dd69a3fd1/rust/src/point/mod.rs#L30-L39 https://github.com/andreaferretti/kmeans/blob/935b8966d4fe0d...) with this single line (https://github.com/andreaferretti/kmeans/blob/master/nim/algo.nim#L10 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).
- Coding_Cat 12y agoFor 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.
- Ygg2 12y ago> 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.
- sanderjd 12y agoA simple example for addition would be adding two u64s and getting a bignum type.
- steveklabnik 12y agoThe last time this post appeared, I actually submitted a PR which did this, among other things: https://github.com/andreaferretti/kmeans/pull/3 https://github.com/andreaferretti/kmeans/pull/3 The post wasn't really updated.
- andreaferretti 12y agoHi, 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
- steveklabnik 12y agoOh 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 :/