July 5, 2021

Is Rust OOP

During my different usages and learning of Rust, I tried to use paradigms from the C++ and Java. After some practice, I was able to reproduce some design patterns in my Rust code. It is a great start to organize my code with interfaces and recognizable patterns! However, Rust is not an Object-Oriented-Programming language… At least, it is the “short answer”, the nature of Rust is complex. Indeed, the OOP deserves an entire chapter in the Rust book!

For this post, I will review with you some characteristics of Rust and why it is or not an OOP language!

What is an OOP Language?

I will try to make a short definition because the term is complex. I already made a post about the OOP and its paradigms here. Do not hesitate to check it!


Is OOP (Object Oriented Programming) dying? Should we stop to use OOP? This question is

Why Oriented Object Programming is considered as dying? What are weaknesses of OOP?

For this post, I will simplify the definition of OOP to the respect of three of the main paradigms:

  • Encapsulation
  • Polymorphism
  • Inheritance 

Concerning Rust

Let review the three pillars of object-oriented programming language in Rust!

Encapsulation

Rust respects the Encapsulation principle! Indeed, it is a simple “struct”:

Polymorphism

Polymorphism is the ability to pass a function that will differ following the object type. In Rust, Polymorphism is possible thanks to the “traits”:

Inheritance

Inheritance is one of the biggest benefits of OOP. The key is to provide code re-usability: Instead of implementing the same code in many places, inheritance allows to copy the property of a class to another one.

Concerning Rust, inheritance is not possible: A structure cannot inherit another one. The reuse of the content and functions is not possible. The only key which is closer to this definition is the declaration of default functions in “Traits” … But it is not enough to call that “Inheritance”!

However, I am not surprised: The Inheritance is a poisoned chalice. Indeed, even if the re-usability of code is a great advantage, it complexes the code structure:

Without discipline, documentation, and coding rules, it can trigger a complex and hard-to-read architecture:

Unclear-Design-Inheritance

Inheritance is beneficial but also one of the most criticized paradigms of OOP.

If Rust is not an OOP, what is it?

Rust is a “multi-paradigm” language. In fact, Rust is inspired by multiple languages with various paradigms. It can concern JavaScript, C/C++, Haskell, Swift and others!

However, be careful, it is hard to write Rust code in an OOP style. If you write it as you are using classes, you can run into problems. Indeed, the “Trait” feature is a great way to mimic OOP style. However, remember that “Trait” had been created to define “contracts”.  It links multiple structures to a common interface.

Do not hesitate to check this quite interesting story about Rust! It is a great way to start this programming language and how it helps you to write secured and clean code in other languages!

Photo by Christina @ wocintechchat.com on Unsplash

About the author 

Axel Fortun

​Developer specialized in Linux environment and embedded systems.
​Knowledge in multiple languages as C/C++, Java, Python​ and AngularJs.
​Working as Software developer since 2014 with a beginning in the car industry​ and then in ​medical systems.