Q:

Rust program to return an array from the function

belongs to collection: Rust Functions Programs

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to pass a structure to the function... >>
<< Rust program to pass an array in a function...