Q:

Rust program to pass an array in a function

belongs to collection: Rust Functions Programs

0

In this program, we will create a user-defined function PrintArray() to accept an array as an argument and print array elements.

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 pass an array in a function is given below. The given program is compiled and executed successfully.

// Rust program to pass an array in a function

fn PrintArray(arr: &mut [i32]) {
    println!("Array Elements: ");
    for i in 0..5 {
        println!("{0} ", arr[i]);
    }
}

fn main() {
    let mut arr:[i32;5] = [10,20,30,40,50];
    
    PrintArray(&mut arr);
}

Output:

Array Elements: 
10 
20 
30 
40 
50

Explanation:

In the above program, we created two functions PrintArray() and main(). The PrintArray() function is a user-defined function that accepts an array of integers and array elements on the console screen.

In the main() function, we created an array of integers with 5 elements. Then we called PrintArray() function and printed the array elements.

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 return an array from the function... >>
<< Rust program to demonstrate the call by reference ...