Start learning free

LESSON 182 OF 414 · PROJECTS: EVERYDAY PROGRAMS

Report Card V6 - Class Statistics in Rust

Practice

Compute three class statistics from a list of scores: the highest, the lowest, and the average.

You start with:

// Rust: the editor starts empty - declare the data yourself, for example:
let mut scores = vec![92, 78, 85, 67, 74, 88, 91, 58];

Build:

Highest: 92
Lowest: 58
Average: 79

Rules:

  • highest is the largest value in scores.
  • lowest is the smallest value in scores.
  • average is the sum of all scores divided by how many there are, rounded down (integer division).
  • Print "Highest: ", "Lowest: " and "Average: " on three lines, in the order of the sample output (println!("Highest: {}", highest);).

Use .iter().max() and .iter().min() which return Option; .iter().sum() for the total.

let mut scores = vec![92, 78, 85, 67, 74, 88, 91, 58];
let mut highest = scores.iter().max().unwrap();
let mut lowest = scores.iter().min().unwrap();
let mut total = 0;
for s in &scores {
    total += s;
}
let mut average = total / scores.len();
println!("Highest: {}", highest);
println!("Lowest: {}", lowest);
println!("Average: {}", average);

STEP 1 OF 10
    Ctrl + Enter to check

    Report Card V6 in Rust

    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free