Problem overview
Many brands sell the same product. Their names are in a vector brands and their ratings in a vector ratings, with ratings[i] belonging to brands[i]. The program scans the lists to find the highest-rated brand and prints it as 1. Brand (rating). If two brands share the top rating, the one listed first wins.
The lesson starts with an empty editor, so you declare the data: five brands, TechPro, MegaCorp, Quantum, NeoByte and Zenith, with ratings 4, 5, 3, 5 and 4. The top rating, 5, appears twice, at positions 1 and 3. MegaCorp is listed first, so the output is:
1. MegaCorp (5)
The previous version compared two brands with an if/else. A list can hold any number of brands, so this version uses a loop.
Approach: a running champion
Keep the position of the best brand seen so far, best_index, and start it at 0: until something better turns up, the first brand is the best. A loop then visits the positions from 1 to the end. For each one, it compares that brand's rating with the rating at best_index, and if it is strictly higher, best_index moves there.
The program stores a position and not the best rating, because one position gives both the brand and its rating: brands[best_index] and ratings[best_index].
The strictness of the comparison implements the tie rule. When a later brand merely equals the current best, the condition is false, so the earlier brand stays in front. That is how MegaCorp keeps its lead over NeoByte.
Step by step
- Declare
brandswithvec!in the given order, since the ratings line up with it by position. - Declare
ratingsas 4, 5, 3, 5, 4, in the same order. - Create
best_indexwithlet mutand the value 0. It needsmutbecause it changes. - Start the loop with
for i in 1..ratings.len(). It begins at 1 because position 0 is already the best so far, and it stops before the length. - Compare in the loop. An
ifchecks whetherratings[i]is strictly greater thanratings[best_index]. - Move the champion. Inside the
if, setbest_indextoi. - Print after the loop using the brand and the rating at
best_indexin the1. Brand (rating)template.
The solution in Rust
let brands = vec!["TechPro", "MegaCorp", "Quantum", "NeoByte", "Zenith"];
let ratings = vec![4, 5, 3, 5, 4];
let mut best_index = 0;
for i in 1..ratings.len() {
if ratings[i] > ratings[best_index] {
best_index = i;
}
}
println!("1. {} ({})", brands[best_index], ratings[best_index]);
Rust details to keep in mind:
- The range is half-open.
1..ratings.len()includes 1 and leaves out the length. For five ratings,itakes the values 1, 2, 3 and 4. - Index type.
best_indexstarts as 0, but because it is used to index vectors, Rust makes it ausize. The loop variable is ausizetoo, sobest_index = icompiles without a cast. - Out-of-range indexing panics. A vector never silently gives a default value. Reading an index past the end stops the program with an
index out of boundsmessage. - Parallel vectors. Nothing in the types ties
brandstoratings. The program relies on both being the same length and the same order.
Complexity
Time: O(n) for n brands, because the loop visits each position once. Space: O(1) extra, because the program keeps one index.
Edge cases and common mistakes
- Ties. With
>=instead of>, the lesson's data prints1. NeoByte (5)instead of MegaCorp, because the later 5 takes over. Two brands that both have rating 3 give the first one, with>. - The best brand is first. With ratings 3, 2 and 1 the condition never holds and
best_indexstays 0. - The best brand is last. With ratings 1, 2 and 3 the loop reaches the last position and prints the third brand, with rating 3.
- One brand. The range
1..1is empty, so the loop doesn't run and the only brand is printed with its rating. - No brands. The loop is skipped, but
brands[best_index]then reads position 0 of an empty vector and the program panics withindex out of bounds: the len is 0 but the index is 0. - Tracking the rating instead of the position. You would then have the best rating but no way to get its brand without searching again.
Testing
- The lesson's data prints
1. MegaCorp (5). - Brands A, B, C with ratings 1, 2, 3 print
1. C (3). - Brands A, B, C with ratings 3, 2, 1 print
1. A (3). - Brands A and B with ratings 3 and 3 print
1. A (3). - A single brand
Solowith rating 2 prints1. Solo (2).
