Rust uses some of the features of functional programming, object orientated programming and uses structures to structure the data.
In this article by Software Consultant Amita Yadav, you can find out exactly what Structure is as well as the functionality of class and objects in Rust.
'Structuring of data in the correct way is very important. In Java and C++, we use Class & Object. In C, we use structures, unions, and enums.
We know that Rust adopts some features of functional programming and OOPS. Class and object is the main weapon of OOPS‘ feature but how rust provides that functionality in its context. We will see this:
As Rust is a bit similar to C, so it also uses structures to structure the data.
What is Structure?
- Classical struct
- Tuple Struct
- Unit Struct
How to create a classical struct?
struct User {
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
To use a struct after we’ve defined it, we create an instance of that struct by specifying concrete values for each of the fields. We don’t have to specify the fields in the same order in which we declared them in the struct. For example,
let user1 = User {
email: String::from("someone@example.com"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};
To get a specific value from a struct, we can use dot notation. You don’t have to rely on the order of the data to specify and access the values of an instance. For example,
let mut user1 = User {
email: String::from("someone@example.com"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};
user1.email = String::from("anotheremail@example.com");
Functionality of Class and Objects in Rust
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.
Functionality of Constructors in Rust
Note that the entire instance must be mutable; Rust doesn’t allow us to mark only certain fields as mutable. As with any expression, we can construct a new instance of the struct as the last expression in the function body to implicitly return that new instance. For example,
impl User {
fn new(email: String, username: String) -> User {
User {
email: email,
username: username,
active: true,
sign_in_count: 1,
}
}
}
Using the Field Init shorthand
When the parameter names and the struct field names are exactly the same, we can use the field init shorthand. For example,
fn new(email: String, username: String) -> User {
User {
email,
username,
active: true,
sign_in_count: 1,
}
}
Creating Instances From Other Instances
let user2 = User {
email: String::from("another@example.com"),
username: String::from("anotherusername567"),
active: user1.active,
sign_in_count: user1.sign_in_count,
};
The syntax .. specifies that the remaining fields not explicitly set should have the same value as the fields in the given instance.
let user2 = User {
email: String::from("another@example.com"),
username: String::from("anotherusername567"),
..user1
};
Type of struct
1. Tuple Structs
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
let black = Color(0, 0, 0);
let origin = Point(0, 0, 0);
2. Unit Structs
struct Nil;
I hope this blog covers all your doubts about how to structure your data in rust as compared to other programming languages.
Thank You!'
This article was written by Amita Yadav and originally posted on Blog Knoldus.