Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
- Dart Programming
- Flutter Tutorial
- Numerical Methods Tutorials
- VueJS
- Go
Practical Paper
Industrial Training
What is Struct?
A structure is a user-defined data type that consists of variables of different data types. A structure is defined by using the struct keyword before the structure name. Structure members are enclosed within the curly brackets. Inside the curly brackets, structure members are defined with their name and type and structure members are also known as fields.
The Syntax of structure:
struct Student { member-variable1; member-variable2; . . }
In the above syntax, structure is defined by using the keyword struct. A structure contains the variables of dissimilar types.
How to declare the instance of a structure
let user = Student{ // key:value pairs; }
In the above declaration, a user is an instance of Student structure. It is defined by using the structure name and then curly brackets. The curly brackets contain key:value pairs where keys are the name of the fields and value is the data which we want to store in the key field.
Let's create a structure of employee:
struct Employee{ employee_name : String, employee_id: u64, employee_profile: String, active: bool, }
An Instance of employee structure:
let employee = Employee{ employee_name : String::from("Akshay Gupta"), employee_id: 12, employee_profile : String::from("Computer Engineer"), active : true, };
How to access a specific member variable of Structure?
We can access the specific member variable of a structure by using dot notation. Suppose we want to access the employee_name variable of an Employee structure, then it looks like:
employee.employee_name;
Note: If we want to change the value of a particular field by using dot notation, then we have to make an instance mutable as Rust does not allow a particular field as mutable.
let mut employee = Employee{ employee_name : String::from("Akshay Gupta"), employee_id: 12, employee_profile : String::from("Computer Engineer"), active : true, }; employee.employee_name = String :: from("Akhil Gupta");
Creating an instance within the function body:
fn create_employee(name:String, profile:String) { Employee{ employee_name:name, employee_id:12, employee_profile:profile, active:true, } }
In the above example, an instance of Employee structure is created implicitly within the function body. The create_employee() function returns the instance of Employee structure with the given name and profile.
Using the Field Init Shorthand when parameters passed to the function and fields have the same name.
Rust provides the flexibility of using field init shorthand when both the variables and fields have the same name. There is no need of repetition of fields and variables.
fn create_employee(employee_name:String, employee_profile:String) { Employee{ employee_name, employee_id:12, employee_profile, active:true, } }
In the above example, the name of parameters and fields are the same. Therefore, there is no need of writing employee_name:employee_name, it can be directly written as employee_name.