Functions With Arrays as Arguments

This lesson discusses functions and arrays in Rust!

It is often necessary to pass arrays as arguments to functions. Rust allows the programmer to pass arrays either by value or by reference.

Pass by Value

Arrays can be passed to a function by value. What that means is that a copy of the array from the calling function is made to the called function.

The general syntax for passing an array by value to a function is:

fn function_name( mut array_name:[datatype;size])

Example 1

The following example takes the array arr by value in the function parameter.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy