Start learning free

LESSON 96 OF 414 · CONDITIONAL STATEMENTS - CHALLENGES

Top K Brands V2 - Better of Two in Rust

Practice

Two brands sell the same product. Show the one with the higher rating.

You start with: an empty editor (no starter code). Declare this data yourself:

let brand1 = "TechPro";
let rating1 = 4;
let brand2 = "MegaCorp";
let rating2 = 5;

Build:

1. MegaCorp (5)

Rules:

  • Use if/else to pick the winner: brand1 when rating1 > rating2, otherwise brand2.
  • Declare best_brand and best_rating without a value (let best_brand;) and assign them in the branches.
  • Format the result as "1. Brand (rating)" and print it with println!(...).

Pick the winner into best_brand and best_rating first, then build the output text from them.

format!("1. {} ({})", brand, rating) builds text from several values, numbers included.

        let brand1 = "TechPro";
        let rating1 = 4;
        let brand2 = "MegaCorp";
        let rating2 = 5;
        let best_brand;
        let best_rating;
        if rating1 > rating2 {
            best_brand = brand1;
            best_rating = rating1;
        } else {
            best_brand = brand2;
            best_rating = rating2;
        }
        println!("1. {} ({})", best_brand, best_rating);

STEP 1 OF 11
    Ctrl + Enter to check

    Top K Brands V2 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