Start learning free

LESSON 71 OF 414 · STRINGS I

Top K Brands V1 - One Brand in Rust

Practice

E-commerce search page: when a shopper searches a product, show the top-rated brand selling it. In v1 there's just one product and one brand.

You start with:

        let product = "Laptop";
        let brand = "TechPro";
        let rating = 4;

Build:

Laptop
1. TechPro (4)

Rules:

  • The first line is the product name.
  • The second line is a ranked entry written as "1. Brand (rating)".
  • Keep each line in its own variable (product_line and brand_line), join them with a newline "\n" and print the result with println!(...).

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

println!("{}\n{}", product_line, brand_line) prints both lines, one per row.

        let product = "Laptop";
        let brand = "TechPro";
        let rating = 4;

        let product_line = product;
        let brand_line = format!("1. {} ({})", brand, rating);
        println!("{}\n{}", product_line, brand_line);

STEP 1 OF 3
    Ctrl + Enter to check

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