Structuring of data in correct way is very important. In java and C++, we use Class & Object. In C, we use structures , unions, and enums.
As Rust is bit similar to C, so it also uses structures to structure the data.
How to create a classical struct?
To create and define a struct,you enter the keyword struct and name the entire struct. A struct’s name should describe the significance of the pieces of data being grouped together. Then, inside curly brackets, we define the names and types of the pieces of data, which we call fields. For example, a struct that stores information about a user account.
Object-oriented programs are made up of objects. An object packages both data and the procedures that operate on that data. The procedures are typically called methods or operations.
Using this definition, Rust is object-oriented: structs and enums have data, and impl blocks provide methods on structs and enums. Even though structs and enums with methods aren’t called objects, they provide the same functionality, according to the Gang of Four’s definition of objects.
The syntax .. specifies that the remaining fields not explicitly set should have the same value as the fields in the given instance.
I hope this blog covers all your doubts about how to structure your data in rust as compared to other programming languages. This article was first published on the Knoldus blog.
Amita Yadav is a Software Consultant Trainee at Knoldus Software INC, currently working on RUST, system programming language. She is self-determined at what she has to do. She likes to learn trending technologies. She is familiar with languages such as C++, C, Java, Scala and technologies like lagom, Play, Akka, Kafka, spark, and databases like Cassandra, MySql, Sqlite, Slick. She loves to create amazing videos mashup.