ADVERTISEMENT
ADVERTISEMENT

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.
Submitted by Nidhi, on October 09, 2021

Problem Solution:

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


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.