Why not just add classes?


#42

I tried to use Rust today, and after Swift this language looks like C with security features. I guess someone must develop Rust++ and add OOP.


#43

I would suggest giving the language a bit more time before emitting this judgement. And looking up traits.


#44

Rust was designed with a “composition over inheritance” point of view, so it’s similar to Go in that respect. It’s most definitely more than just a “C with security features”, Rust is more like a high level programming language which lets you use a lot of the powerful functional programming patterns you may use in Haskell, Python, or JavaScript, while also giving you the ability to do low level/high performance things like you would in C++.

In particular you may find this quote from the second post in this thread interesting:


#45

HadrienG, Michael-F-Bryan, I got your point, but I just expected Rust to be more high level language than it is.
Syntax for OOP in Rust reminds me C-style implementations like GObject.
I am not saying that inheritance is necessary, or Java-style OOP is always right, but from Swift or Kotlin programmer OOP syntax looks archaic, and it’s going to be hard for not C-people to transfer to Rust.

Swift:

class MessagePresenter {
	func show(message: String) {
		print(message)
	}
}
let messagePresenter = MessagePresenter()
messagePresenter.show(message: "Hello World!")

Rust:

struct MessagePresenter {
}
impl MessagePresenter {
    pub fn show(&self, message: String) {
        println!("{}", message);
    }
}
fn main() {
    let message_presenter = MessagePresenter{};
    message_presenter.show(String::from("Hello World!"));
}

#46

it does seem like layering traits can often handle the use cases of inheritance but in a more general manner (a trait with a default impl relying on another with a load of getters/setters isn’t far of)… perhaps there’s some tutorial somewhere that would spell it out… ‘here’s something done with inheritance, here’s the approach with traits’).

i am sympathetic to both POVs on this… with traits/generics/enums as the starting point you’ve wiped out some of the use cases of inheritance… you can simplify some interfaces by switching on enums. you can make generic ‘impls’ that seem to achieve ‘layered interfaces’ in quite a pleasing way. ‘here’s the interface for a window… here’s the interface for a grid view… here’s a generic allowing anything satisfying grid view to satisfy window’)

on the other hand inheritance could be seen just as a handy syntactic shortcut in the case of ‘1 struct, 1 interface’ (which you could still expand upon), and ‘an embedded vtable’ is a means of having a polymorphic object of variable size , located by a single pointer. (enums are padded … it might be interesting if rust eventually gains an ability to express an immutable-tag enum without the padding)


#47

@demensdeum, I’m not sure exactly what you mean by “OOP” here. My interpretation of object-oriented programming is that you can have types which bundle some data and also allow you to call methods on that type which let you do operations on the bundled data. Whether you use the class keyword or struct is just a syntax detail.

The big thing with OOP is the ability to have polymorphism, i.e. being able to use one type in the place of another type transparently as long as they specify some sort of interface. Traits and inheritance allow you to do this, it’s just implemented in different ways. Rust does things in a different way to C++ or Java (which is why it’s called “Rust”, not “C++++”), but I wouldn’t necessarily say one way of doing things is better or another is archaic.

I think you’ll find that differences like methods taking an explicit self parameter instead of the implicit this in Java or C++, or how you’ll define the contents of a type separate from the behaviour (the separate impl MessagePresenter block) are only skin deep. You still often use similar patterns to “normal” OO languages, it’s just that you’ll use composition more and not have a deep type hierarchy.

I’d agree with @HadrienG. It always takes a bit of time to learn a new language and you often experience a bit of friction because you come in with a bunch of assumptions from your old language and find that those patterns aren’t idiomatic in the new one. Before learning Rust I did a lot of work with Python and C++, and it took me a while to get used to how Rust does things.

My advice would be to go along with it for a bit longer, try reimplementing some of your old applications in Rust to get a hang of the language, and don’t be afraid to ask questions like “is this idomatic?”, and try that for a couple weeks before deciding it’s not high level enough. For example, here’s a library for generating LaTeX documents I made. You’d be hard pressed to find something more high-level in Python or Ruby.