The source code to pass a structure into the function is given below. The given program is compiled and executed successfully.
// Rust program to pass a structure
// into the function
struct Employee {
id:i32,
name:String,
class:String
}
fn printEmp( emp:Employee){
println!("Id:{}, Name:{}, Class:{}",emp.id,emp.name,emp.class);
}
fn main() {
let emp = Employee {id:101,name:String::from("Rohit"),class:String::from("MCA")};
printEmp(emp);
}
Output:
Id:101, Name:Rohit, Class:MCA
Explanation:
In the above program, we created two functions printEmp() and main(). The printEmp() function is a user-defined function that accepts structure as a parameter and prints the member of structure on the console screen.
In the main() function, we created the object of structure Employee and passed it to the printEmp() function, and printed employee information.
Program/Source Code:
The source code to pass a structure into the function is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created two functions printEmp() and main(). The printEmp() function is a user-defined function that accepts structure as a parameter and prints the member of structure on the console screen.
In the main() function, we created the object of structure Employee and passed it to the printEmp() function, and printed employee information.
need an explanation for this answer? contact us directly to get an explanation for this answer