The source code to create a structure with Default values is given below. The given program is compiled and executed successfully.
// Rust program to create a structure
// with default values
#[derive(Default)]
struct Employee {
eid:u32,
name:String,
salary:u32
}
fn main() {
let mut emp:Employee= Employee::default();
println!("Employee Information");
println!(" Employee ID : {}",emp.eid );
println!(" Employee Name : {}",emp.name);
println!(" Employee Salary: {}",emp.salary);
}
Output:
Employee Information
Employee ID : 0
Employee Name :
Employee Salary: 0
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 printed the result.
Program/Source Code:
The source code to create a structure with Default values is given below. The given program is compiled and executed successfully.
Output:
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 printed the result.
need an explanation for this answer? contact us directly to get an explanation for this answer