3 ms·
You're right, but I find Rusts concepts/keywords also a bit confusing. I know what a class and what an interface is, but Rust doesn't have these. It has a stru
by k__ 6y ago
You're right, but I find Rusts concepts/keywords also a bit confusing.
I know what a class and what an interface is, but Rust doesn't have these. It has a struct, which is kinda like a class without methods? It has a trait, which is kinda like an interface but with implementations for methods? It has ... implementations... which kinda make a struct to a class, but not really.
I don't mean to hate here, but these are all things that need to be understood in some kind of way.
- kzrdude 6y agoOnce understood, even taken together, none of those have as many rules baked into one concept as "class" does; each is simple.
- judofyr 6y agoI quite like the trait system in Rust, but I'm not sure if I would argue that it's simpler than a class-based language. Just looking at `foo.bar()` there are similar types of complexities. In a class-based language you need to be aware of the class hierarchy, and in Rust you need to be aware of imported traits and auto dereferencing.
- estebank 6y agoI honestly think this is a factor of prior experience. When I started using Rust my background was in OOP languages, so I also went through the exercise of fitting trait pegs into a class shaped holes and it was painful. But this doesn't mean that traits and ADTs and how they interact is harder to learn or understand than Objects with its inheritance, polymorphism, encapsulation and abstraction concepts, it just means that if you already know those, you will have to learn new concepts to learn Rust, which can be surprising if you aren't told about it ahead of time, particularly when you already know several languages that don't use traits.
- k__ 6y agoI did learn much about classes and interfaces in university, independent of language. But not much on traits and ADTs(?). Can you recommend some general learning resources on that?
- estebank 6y agoI do not have any non-Rust resources off the top of my head, but I can give you some quickly looked up resources and enough phrases to search for that should help you in this endeavor. ADTs are Abstract Data Types[1][2][3]: > ADT is implementation independent. For example, it only describes what a data type List consists (data) and what are the operations it can perform, but it has no information about how the List is actually implemented. In the context of Rust it means that the traits, structs and enums are ADTs, while the impls are Data Structures. Having structs and enums be the way they are in Rust (simplistic and with little extensibility beyond implementing traits) is that pattern matching[4][5] and destructuring is cheap and built in. Pattern matching becomes specially useful when combined with sum types/tagged unions/enums[6][7][8]. On the other corner you have Scala which lets you implement specific interfaces to allow structuring and destructuring for arbitrary types, but that has the same problems as overriding constructors in C++: the performance implications of pattern matching is impl dependent and hidden at the point of calling. [1]: https://softwareengineering.stackexchange.com/questions/148747/abstract-data-type-and-data-structure https://softwareengineering.stackexchange.com/questions/1487... [2]: https://en.wikipedia.org/wiki/Abstract_data_type https://en.wikipedia.org/wiki/Abstract_data_type [3]: https://abrickshort.wordpress.com/2005/03/06/abstract-data-types-vs-data-structures/ https://abrickshort.wordpress.com/2005/03/06/abstract-data-t... [4]: https://en.wikipedia.org/wiki/Pattern_matching https://en.wikipedia.org/wiki/Pattern_matching [5]: https://docs.scala-lang.org/tour/pattern-matching.html https://docs.scala-lang.org/tour/pattern-matching.html [6]: https://www.schoolofhaskell.com/school/to-infinity-and-beyond/pick-of-the-week/sum-types https://www.schoolofhaskell.com/school/to-infinity-and-beyon... [7]: https://chadaustin.me/2015/07/sum-types/ https://chadaustin.me/2015/07/sum-types/ [8]: https://stackoverflow.com/questions/2502354/what-is-pattern-matching-in-functional-languages https://stackoverflow.com/questions/2502354/what-is-pattern-...
- k__ 6y agoLess rules doesn't mean simpler. Also, I don't mean Rust is harder than C++ or Java. It's just quite different. If you are used to visualizing software as graphs of functions or classes in your mind for years, switching to the Rust model is quite a change of thinking.