Rust program to return an array from the function

Rust | Returning array from function: Write an example to demonstrate returning an array from the function.
Last Updated : October 09, 2021

Problem Statement

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

Program/Source Code

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

// Rust program to return an array 
// from the function

fn GetArray()->[i32;5] {
    let mut arr:[i32;5] = [10,20,30,40,50];
    
    return arr;
}

fn main() {

   let arr = GetArray();
   println!("Array Elements: ");
	for i in 0..5 {
		println!("{0} ", arr[i]);
	}
}

Output

Array Elements: 
10 
20 
30 
40 
50

Explanation

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

In the main() function, we called GetArray() function to get the array of integers. Then we printed the returned array.

Rust Functions Programs »



Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

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