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.
Program/Source Code:
The source code to return an array from the function is given below. The given program is compiled and executed successfully.
Output:
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.
need an explanation for this answer? contact us directly to get an explanation for this answer