Rust Adventures — From Java Class to Rust Struct

Floriano Victor Peixoto
Analytics Vidhya
Published in
9 min readMay 25, 2020

--

Hi there folks!

As you already know I´m learning Rust as one of my goals to 2020. The major of my experience was with Java up until now, although I played with some others. So the Object-Oriented Programming is part of my daily life.

Classes are in the core of Java, there is simply no way to write a Java program without a class, even to print a Hello World you will need a class.

But this concept does not exists in Rust, but some concepts of it can be for sure from OOP, but how?

Structs

Rust has a great variety of data structures, one of them is tuples that is a collection of values that can be accessed by their positions. Here is an example:

This simple program creates a tuple and print its contents. As you can see, we can attach different data types inside a tuple and access it by its current position. The result of running the program will be the following:

Tuples can be used for simple operations or when the meaning of each position is well known, otherwise it can be very difficult to maintain and develop further improvements.

Struct is similar to a tuple, but it needs a name to the data structure and a name for its fields. So we can change our tuple to a struct:

--

--