Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> It's a bit longer than the class example, but conceptually cleaner because multiple structs can implement the trait, some of them without a weight

Sounds like premature abstraction. Instead of declaring a class, you have declared an interface.



There are no classes in Rust, and it's better for it. That's my point, some abstractions like classes are NOT necessary.

There were arguments before Rust 1.0 that people will simply try to emulate classes. But nobody actually bothered, because traits provide enough functionality that you don't actually need to have classes to actually write maintainable and understandable programs.

I'm attacking the exact point of the article that you need X. Maybe you actually don't need X, and your programs will be better for it.


> There were arguments before Rust 1.0 that people will simply try to emulate classes. But nobody actually bothered, because traits provide enough functionality that you don't actually need to have classes to actually write maintainable and understandable programs.

It's not only traits. Traits (well, traits objects) are used for dynamic dispatch, traits themselves are used as composable interfaces, and structs + impl blocs are used for basic classes.

My point was that your example, implementing a trait that may or may not use a weight for barking, is not making the program better. It's making it more complex for no good reason. Here's what your program could be: https://play.rust-lang.org/?version=stable&mode=debug&editio...

Adding traits doesn't make the code better. There's only dogs that are barking. You don't even need to have bark() be a method, a plain function will do.


Of course it can be as simple as that, that's beside the point

But almost every time you actually won't ship a program that just prints stuff to the console

That's why I did it that way, because how you actually output won't be println, but probably to a window or a socket, or whatever

The core functionality of passing a slice will be actually useful in those cases, you can include this code in your final program


> There's only dogs that are barking.

   class DogSoundFile
   constructor Create(MP3File, Weight);
   ...
Bit silly, innit? (Are bigger MP3 files heavier...? :-)




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

Search: