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
- Name the first line. Create
product_lineand set it toproduct. The first line of output is just the product name. - Build the ranked entry. Create
brand_linewithformat!. The template holds the literal rank1., a placeholder for the brand, a space and an opening parenthesis, a placeholder for the rating and a closing parenthesis. - Print both lines. Use one
println!with two placeholders and a newline,\n, between them. The newline ends the first line, andprintln!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 aStringto keep in a variable, which is exactly whatbrand_lineneeds.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.
productis a&str, a borrowed string literal.brand_lineis aString, which owns its text. Both print with{}, so the difference doesn't matter here. \nis 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 reports2 positional arguments in format string, but there is 1 argument. - Using
+to join text and numbers."1. " + brandis rejected withcannot add `&str` to `&str`, because+works only when the left side is aString. 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. Useprintln!, or put\nin 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, brandTechPro, rating 4 printsLaptopand then1. TechPro (4). - Change the rating to 5 and only the number in the parentheses changes.
