5 ms·
Rust calls them "structs". They're like classes, but different. Rust also has "traits". They're like abstract classes, but different.
by Animats 10y ago
Rust calls them "structs". They're like classes, but different. Rust also has "traits". They're like abstract classes, but different.
- Manishearth 10y agoTraits are closer to interfaces than abstract classes, iirc abstract classes can have fields.
- saosebastiao 10y agoI've noticed this and while I don't use it much it has caught me off guard once or twice. And while it wasn't the end of the world, I wasn't too fond of the solutions I came up with. Is there a reason why they can't have fields? Why wouldn't they just be an "extension" to a struct/enum, modifying their memory representation?
- Manishearth 10y agoRust doesn't do single inheritance, basically. If they had fields, what would you do when you implemented two of them on a struct? It also muddles up the ability to tack on traits in later crates. Traits aren't supposed to be used the way you do single inheritance. Rust prefers composition over inheritance, so you combine structs and enums to get what you want there.
- steveklabnik 10y ago> Is there a reason why they can't have fields? The design isn't done yet: https://github.com/rust-lang/rfcs/pull/1546 https://github.com/rust-lang/rfcs/pull/1546
- Manishearth 10y agoNote that this is different from what's being asked, what's being asked is why they can't have fields like abstract classes that get appended to the original struct. That would make traits more like mixins. That RFC lets you declare fields that the trait implementor is supposed to provide; implementing a trait will not automatically add a field, instead, you will be forced to add such a field yourself (unless one already exists) and link it to the trait field. It's a more rusty way of addressing many of the same use cases, but it's not the same thing. It's basically sugar for declaring a getter and setter as trait/interface methods.
- saosebastiao 10y agoWhile it's not what I asked, I actually like the answer as long as I can provide overrideable default methods in the that use the lvalues. I can't think of any of my own use cases that couldn't adequately be covered by it.
- steveklabnik 10y agoAh yeah, I read your post wrong, my bad!
- Manishearth 10y agoOh, yeah, like I said it solves many of the same use cases, it's just not the same thing :)
- k__ 10y agoStructs are for data and traits are for methods?
- ww520 10y agoStruct is for data and Struct's implementation is for methods. Trait is like interface, just a set of function signatures.
- k__ 10y agoWhy isn't it called interface then?
- paavohtl 10y agoBecause they are traits [1], not interfaces, and they also have some features from type classes [2]. When you create a class in an OOP language, you have do declare all interfaces it supports, and provide implementations for them (or mark the class as abstract). In Rust's case, you declare the data separately as a struct declaration, and then implement the traits you want in impl declarations. You can also implement traits for other traits, or for types from other modules, including the standard library. Finally, compared to traditional OOP interfaces Rust's traits can require static functions in addition to instance methods and they can also implement methods with an overridable default implementation. [1] https://en.wikipedia.org/wiki/Trait_(computer_programming) https://en.wikipedia.org/wiki/Trait_(computer_programming) [2] https://en.wikipedia.org/wiki/Type_class https://en.wikipedia.org/wiki/Type_class
- k__ 10y agoAh, I read the Wiki article and my impression of traits was "interfaces with implementation" and as far as I could tell, Rust traits have no implementation, they 'need' impl(emendations) so they seemed more like interferences to me :)
- steveklabnik 10y ago