Sitemap
Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Follow publication

Member-only story

Rust Adventures — From Java Class to Rust Struct

--

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:

--

--

Analytics Vidhya
Analytics Vidhya

Published in Analytics Vidhya

Analytics Vidhya is a community of Generative AI and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Floriano Victor Peixoto
Floriano Victor Peixoto

Responses (1)