Problem overview
Given a vector of scores, letter_grades returns a vector of letters, one per score and in the same order. The ranges are 90 to 100 for "A", 80 to 89 for "B", 70 to 79 for "C", 60 to 69 for "D", and 0 to 59 for "F". The work is split in two: the helper letter_grade turns one score into its letter, and letter_grades applies it to every score. For [95, 82, 71] the result is ["A", "B", "C"], and for [100, 90, 89] it is ["A", "A", "B"]: 90 is the lowest A, and 89 is one point short of it.
Approach: check from the top, one range at a time
The helper tests the highest range first. Because return ends the function, every later check only runs for scores that failed the earlier ones. After the test for 90, a later score >= 80 can only be true for 80 to 89, so each condition needs just one bound. Checking from the bottom would need an upper bound on every test as well.
The second function is a plain collecting loop: start with an empty vector, push one letter for each score, and return the vector after the loop.
Step by step
- Test for A. If
scoreis at least 90, return"A".to_string(). - Test for B, C and D. Three more
ifstatements, for 80, 70 and 60, in that order. - Fall through to F. If no test matched, the score is below 60, so the last line returns "F" with no condition.
- Start the result. In
letter_grades,let mut grades = Vec::new();makes an empty vector. It ismutbecause letters are pushed into it, and Rust infers its element type from the firstpush. - Loop over
&scores. The loop runs once per score, so each score gives one letter. - Push the helper's answer.
Self::letter_grade(*score)returns the letter, andgrades.push(...)adds it at the end, which keeps the original order. - Return after the loop. A
returninside the loop would hand back after the first score.
The solution in Rust
impl Solution {
fn letter_grade(score: i32) -> String {
if score >= 90 {
return "A".to_string();
}
if score >= 80 {
return "B".to_string();
}
if score >= 70 {
return "C".to_string();
}
if score >= 60 {
return "D".to_string();
}
return "F".to_string();
}
fn letter_grades(scores: Vec<i32>) -> Vec<String> {
let mut grades = Vec::new();
for score in &scores {
grades.push(Self::letter_grade(*score));
}
return grades;
}
}
Rust details worth knowing:
&stragainstString. A literal such as"A"is a&str, a borrowed piece of text. The helper's return type isString, an owned, growable string, and Rust does not convert one into the other on its own.return "A";fails withexpected `String`, found `&str`, and.to_string()is the fix.*score. Looping over&scoresgivesscoreas a&i32. The helper takes ani32, so the call passes*score, the number behind the reference.- Separate variables. The helper's parameter is also called
score, but it is a different variable from the loop's. Each function has its own. Self::. Calling another function of the sameimplthat takes noselfneedsSelf::before its name.
Following two scores through the helper
Take 82. It is not at least 90, so the first if is skipped. It is at least 80, so the second one returns "B" and the later tests never run. Now take 40. It fails the tests for 90, 80, 70 and 60 one after another, and reaches the last line, which returns "F". Every score ends at exactly one return, so the helper always produces exactly one letter. That is also why the function compiles without an else anywhere: the final return covers whatever the earlier tests did not catch.
Why split the work in two
The helper knows about one score and nothing about vectors, and the loop knows about vectors and nothing about grade boundaries. If the cut-offs changed, only letter_grade would need an edit. If the input became a different collection, only the loop would. Each function can also be tried on its own: letter_grade(89) returns "B" and letter_grade(90) returns "A", which is a quick way to check the boundaries before running the whole vector through.
The loop itself never decides anything about grades. It forwards each score to the helper and stores whatever comes back, so the output always has exactly as many letters as the input has scores.
Edge cases and common mistakes
- Boundaries. The scores 59, 60, 69, 70, 79, 80, 89 and 90 sit right at the edges. Using
>instead of>=would turn 90 into a B. - Order of the tests. If the test for 60 came first, every passing score would be a D. The checks must run from the highest range down.
- Returning inside the loop. Only the first letter would come back, and the function would not match the vector it should return.
- Lowest and highest scores. 0 falls through to "F", and 100 is covered by
score >= 90.
Testing
[95, 82, 71]gives["A", "B", "C"].[65, 40]gives["D", "F"].[100, 90, 89]gives["A", "A", "B"].[0, 59, 60, 69, 70, 79, 80, 89]gives["F", "F", "D", "D", "C", "C", "B", "B"].
