To find the range of all elements in an array in Rust, you can iterate over the array and keep track of the minimum and maximum values. Here's an example implementation:
main.rs470 chars26 lines
In this example, we define a function find_range
that takes an array of i32
as an argument (&[i32]
).
The function first checks if the array is empty, returning None
if it is. It then initializes two variables, min
and max
, to the first element of the array.
The function then iterates over the remaining elements of the array, updating the min
and max
values as necessary.
Finally, the function returns the range as an Option
containing a tuple of (i32, i32)
representing the minimum and maximum values, respectively.
In the main
function, we define an example array and call the find_range
function on it, printing the result to the console.
gistlibby LogSnag