From the course: Rust Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Solution: Max, min, mean

Solution: Max, min, mean - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Solution: Max, min, mean

(electronic music) [Instructor] To determine the max and min values in the array, I decided to use a for loop to iterate through the array to check each number and see if it's greater than the max value we've encountered thus far, or less than the minimum value, and update those variables accordingly. I'll create that for loop with for num, in numbers.iter, and then, within the loop, I'll use an if expression to check if the current number is greater than max. If so, I'll update the max by assigning it that value. And then, if the current number was not greater than max, perhaps it was less than min, so I'll use an else/if clause to check for that condition. And if so, I'll assign the number to min. Now, for these two comparison operations to work, max and min both need to have an initial value to compare with the current number. So, before the loop begins, I'll initialize both of those variables to the first number in…

Contents