How can I implement the following class structure

How can I implement the following class structure in Rust?

Set of class to represent 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.

Thank you,

1 Like

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.

6 Likes

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.

3 Likes

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 :slight_smile: ( 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.

1 Like

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.