Start learning free

LESSON 192 OF 293 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V1 in Rust: A One-Item Receipt

Practice Read the full explanation

Practice variables, arithmetic, and string concatenation by printing a tiny one-item receipt.

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

let item = "Coffee Mug";
let price = 12;
let quantity = 3;

Build:

Coffee Mug x3
Total: $36

Rules:

  • Compute total = price * quantity.
  • First line: "<item> x<quantity>" (e.g. "Coffee Mug x3").
  • Second line: "Total: $<total>".
  • Join the two lines with a newline "\n" into one string result, then print it with println!("{}", result).

Use format!("{} x{}", item, quantity) and format!("Total: ${}", total) to build strings from values.

format!("{}\n{}", line1, line2) joins both lines into one String.

        let item = "Coffee Mug";
        let price = 12;
        let quantity = 3;
        let total = price * quantity;
        let line1 = format!("{} x{}", item, quantity);
        let line2 = format!("Total: ${}", total);
        let result = format!("{}\n{}", line1, line2);
        println!("{}", result);

STEP 1 OF 8
    Ctrl + Enter to check

    RUST · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V1 in Rust, explained

    Problem overview

    The first version of the Shopping Cart project prints a tiny receipt for one item. You declare three values yourself: the item name Coffee Mug, a price of 12 and a quantity of 3. The program then prints two lines: the item with its quantity, and the total cost.

    Coffee Mug x3
    Total: $36
    

    The lesson practises variables, arithmetic and building text from pieces. The total is the price multiplied by the quantity, and the two lines are joined with a newline into one string called result, which is printed once at the end.

    Approach: compute, format, join, print

    The work splits into four small jobs, each stored in its own variable:

    1. Declare the input data.
    2. Compute total from the price and the quantity.
    3. Build each line of the receipt as text with format!.
    4. Join the lines into result and print it.

    Splitting the work this way keeps every variable simple, and it is the shape that bigger receipts follow later: compute the numbers, format each row, join, print.

    Step by step

    1. Declare item as the text "Coffee Mug", in quotes.
    2. Declare price as 12. It is in whole dollars, so a plain integer is enough.
    3. Declare quantity as 3.
    4. Compute total as price * quantity: one mug's price repeated for each mug.
    5. Build line1 with format!. The template has a placeholder for the item, then the literal x, then a placeholder for the quantity.
    6. Build line2 the same way: the label Total: $ followed by a placeholder for total.
    7. Join the rows into result. Another format! puts a newline, \n, between line1 and line2.
    8. Print result with println!, so each row appears on its own line.

    The solution in Rust

            let item = "Coffee Mug";
            let price = 12;
            let quantity = 3;
            let total = price * quantity;
            let line1 = format!("{} x{}", item, quantity);
            let line2 = format!("Total: ${}", total);
            let result = format!("{}\n{}", line1, line2);
            println!("{}", result);
    

    Rust details worth knowing:

    • Type inference. price and quantity are whole-number literals, so Rust infers i32 for them, and total is an i32 too. Multiplying two i32 values gives an i32.
    • Text and numbers don't mix with +. Writing "Total: $" + total fails with cannot add `{integer}` to `&str`. The + operator isn't how Rust joins text with other values. format! handles any value that can be displayed.
    • Placeholders fill in order. In "{} x{}", the first {} takes item and the second takes quantity. The x between them is literal text, and there is a space before it.
    • $ is not special. Only curly braces mean something in a format string, so Total: ${} prints a dollar sign and then the number.
    • Immutable by default. None of the variables use mut, because none of them changes after it is created.

    Edge cases and common mistakes

    • Spacing. The first row is Coffee Mug x3, with a space between the name and the x but none between x and the number. The second has one space after the colon, before the dollar sign.
    • Joining with the newline. The \n inside the template is what splits the receipt into two rows. Without it, result would hold Coffee Mug x3Total: $36 on one line.
    • Other quantities. The code isn't tied to 3: with a quantity of 1 it prints Coffee Mug x1 and Total: $12.
    • Wrong operation. Adding price and quantity gives 15, not 36, and still compiles. Check the total against a number you can work out by hand.
    • Large values. An i32 tops out at 2,147,483,647. A price of 1,000,000,000 with a quantity of 3 would overflow, which the compiler rejects when the values are constants. This lesson's amounts are tiny.

    Testing

    • Price 12 and quantity 3 print Coffee Mug x3 and Total: $36.
    • Price 12 and quantity 1 print Coffee Mug x1 and Total: $12.
    • Price 5 and quantity 0 print Coffee Mug x0 and Total: $0.
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free