Start learning free

LESSON 71 OF 293 · STRINGS I

Top K Brands V1 in Rust: Printing One Ranked Brand

Practice Read the full explanation

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

    RUST · STRINGS I

    Top K Brands V1 in Rust, explained

    Problem overview

    This is the first version of the Top K Brands project. A shopper searches for a product, and the page shows the brands that sell it, best rated first. In this version there is one product and one brand, so the output is only two lines: the product name, then a ranked entry written as 1. Brand (rating).

    With the product Laptop, the brand TechPro and the rating 4, the program prints:

    Laptop
    1. TechPro (4)
    

    Approach: one variable per line

    The lesson asks you to keep each output line in its own variable, product_line and brand_line, and to print them together at the end. That looks like extra work for two lines, but it is the habit the later versions rely on. Once the program has many brands, each line is built separately and the lines are joined at the end.

    The first line is the product name as it is. The second line mixes text and a number, so it needs to be built from pieces. In Rust that is the job of format!, which takes a template with {} placeholders and one value per placeholder, in order, and returns a String. Numbers can go straight into a placeholder with no conversion.

    Step by step

    1. Name the first line. Create product_line and set it to product. The first line of output is just the product name.
    2. Build the ranked entry. Create brand_line with format!. The template holds the literal rank 1. , a placeholder for the brand, a space and an opening parenthesis, a placeholder for the rating and a closing parenthesis.
    3. Print both lines. Use one println! with two placeholders and a newline, \n, between them. The newline ends the first line, and println! adds a final newline of its own.

    The solution in Rust

            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);
    

    Some details that matter in Rust:

    • format! returns, println! prints. The two macros use the same template language. format! gives you a String to keep in a variable, which is exactly what brand_line needs. println! sends the text to the output.
    • Parentheses in the template are plain text. Only curly braces are special. The ( and ) around the second placeholder appear in the output unchanged. To print a literal brace you double it: {{.
    • Two kinds of text. product is a &str, a borrowed string literal. brand_line is a String, which owns its text. Both print with {}, so the difference doesn't matter here.
    • \n is an escape inside the string. It becomes a single newline character. Printing the joined text with {:?} would show it as "Laptop\n1. TechPro (4)", with the newline visible as \n.

    Edge cases and common mistakes

    • Too few arguments. A template needs one value per placeholder. format!("1. {} ({})", brand) doesn't build, and the compiler reports 2 positional arguments in format string, but there is 1 argument.
    • Using + to join text and numbers. "1. " + brand is rejected with cannot add `&str` to `&str`, because + works only when the left side is a String. Adding a number to text is rejected too. format! avoids both problems.
    • Printing the lines in two separate calls without a newline. print! doesn't end the line, so the output would run together. Use println!, or put \n in the template as this lesson does.
    • Space and punctuation. The output must match exactly: a dot and a space after the rank, a space before the opening parenthesis and no space inside the parentheses.

    Testing

    • Product Laptop, brand TechPro, rating 4 prints Laptop and then 1. TechPro (4).
    • Change the rating to 5 and only the number in the parentheses changes.
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free