How can I implement the following class structure in Rust?
It is difficult for me overall because the class structure has:
- classes that extends from other classes, e.g., Data extend from Element.
- some Abstract classes, e.g., Element, or Data.
It is unlikely that exactly mirroring this class structure is the best way to translate it into Rust. I would need more details about how it is used to give more specific advice.
Short answer is: You can't. Rust is not an object oriented language. It has no notion of "class" or "inheritance" built in.
Having said that, I bet there are those who could suggest achieving something akin to classes and inheritance in Rust. Albeit it in some verbose, clunky, fashion. As one can in languages like C even.
Better would be to reconsider if a class/inheritance based solution is necessary and hence how to do it in a more Rusty way.
More information on what the actual problem is would probably help.
Dear Alice and ZiCog,
Thank you for your answer.
I think I understand the direction of your answers.
Do not to think in class/inheritance but in behaviours for the struct.
Let see what I can do ( Rust Playground )
PD: Sorry, because I can not give more details.
In general Rust favors composition over inheritance, so it's not unlikely that the solution to this problem will involve plenty of composition.
It's though to say more without specifics, but in a vacuum I can imagine that every class
C inheriting from a superclass
S is replaced 1:1 by a struct with a field
f: P where
P is a Rust type that corresponds to
S in your java code. This may or may not work for your code base.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.