ADVERTISEMENT
ADVERTISEMENT

Rust program to return a structure from the function

Rust | Returning structure from function: Write an example to demonstrate the example of returning a structure from the function.
Submitted by Nidhi, on October 09, 2021

Problem Solution:

In this program, we will create a user-defined function to return a structure to the calling function and print the member of the structure.

Program/Source Code:

The source code to return a structure from the function is given below. The given program is compiled and executed successfully.

// Rust program to return a structure 
// from the function

struct Employee {
   id:i32,
   name:String,
   class:String
}

fn getEmp()->Employee{
    let emp = Employee {id:101,name:String::from("Rohit"),class:String::from("MCA")};
    return emp;
}

fn main() {
    let emp=getEmp();
    println!("Id:{}, Name:{}, Class:{}",emp.id,emp.name,emp.class);
}

Output:

Id:101, Name:Rohit, Class:MCA

Explanation:

In the above program, we created two functions getEmp() and main(). The getEmp() function is a user-defined function that returns the object of structure Employee to the calling function.

In the main() function, we called the getEmp() function and get the object of Employee structure, and printed the employee information.

Rust Functions Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


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.