ADVERTISEMENT
ADVERTISEMENT

Rust program to create a structure and assign the values to the members

Rust | Structure Example: Write a program to create a structure and assign the values to the members.
Submitted by Nidhi, on October 27, 2021

Problem Solution:

In this program, we will create a structure with few members. Then we will create an object of created and initialize structure with the default value. After that, we will assign values member-wise using the "." operator.

Program/Source Code:

The source code to create a structure and assign the values to the members is given below. The given program is compiled and executed successfully.

// Rust program to create a structure and 
// assign the values to the members

#[derive(Default)]

struct Employee {
   eid:u32,
   name:String,
   salary:u32
}

fn main() {
   let mut emp:Employee= Employee::default();
   
   emp.eid     = 101;
   emp.name    = String::from("Lokesh Singh");
   emp.salary  = 50000;
   
   println!("Employee Information");
   println!("  Employee ID    : {}",emp.eid );
   println!("  Employee Name  : {}",emp.name);
   println!("  Employee Salary: {}",emp.salary);
}

Output:

Employee Information
  Employee ID    : 101
  Employee Name  : Lokesh Singh
  Employee Salary: 50000

Explanation:

In the above program, we created a structure Employee and a function main(). The Employee structure contains three members eid, name, and salary.

In the main() function, we created the object of structure and initialized it with default values using the default() method. After that, we assigned values member-wise using the "." operator and printed the result.

Rust Structures Programs »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.