Rust program to return multiple values from the function

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

Problem Solution:

In this program, we will create a user-defined function to return multiple values to the calling function.

Program/Source Code:

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

// Rust program to return multiple values 
// from the function

fn myfun()->(i32,i32){
    return (10,20);
}

fn main() {
    let (num1,num2)=myfun();
    
    println!("Num1: {}",num1);
    println!("Num2: {}",num2);
}

Output:

Num1: 10
Num2: 20

Explanation:

In the above program, we created two functions myfun() and main(). The myfun() function is a user-defined function that returns two integer values to the calling function.

In the main() function, we called the myfun() function and get two integer numbers, and printed them.

Rust Functions Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.