Lesson 4

Basic macros

println!, format!, vec!, and assert! — the four macros you will see in almost every Rust program.

Level
Beginner
Duration
18 min
Updated

Four macros you already used, now explained

You already wrote println! in almost every example in this course, and every exercise checked your solution with assert_eq!, without ever explaining what they are or why they carry !. This lesson closes that gap, and along the way closes out module 1 entirely: vec! delivers on the promise “Variables, mutability and types” made about showing a collection that can actually grow.

println!, format!, vec!, and assert! are macros, not functions: that is why they carry !. A macro expands at compile time, so it can accept a variable number of arguments and check your format string before the program even runs.

You will go through four stations, one per macro: println!, format!, vec!, and the assert! family.

Macro state

  • CALL · println!macro, not function: note the !
  • MACRO · Display vs Debug{scores:?} prints the array
  • RESULT · printed output3 apples · [1, 2, 3]
01 / 04{} · {:?} · {NAME}

Exercise 01 · Score report

Build a report with vec! and format!.

The starter already declares the signature. Complete the body with a Vec, a for loop to sum, and format! to build the final text.

0/1correct

Complete build_report so it returns a String in the format "scores: [a, b, c], total: sum", using vec!, a for loop to sum, and format!.

01

Implement

Replace `todo!` with `vec![a, b, c]`, a `for` loop that sums, and `format!` with `{scores:?}` and `{sum}`.

Function 01

Build the report

Unchanged

Group the three values in a Vec, sum them with a for loop, and format the result with format!.

pub fn build_report(a: i32, b: i32, c: i32) -> String {
    todo!("build a Vec with vec![a, b, c], sum it with a for loop, and format the result")
}
02

Check

Run the visible cases and use the diagnostic to correct the code.

produces the exact text for both positive and negative values
Compiler

Modify the code before running the tests.

03

Help

View progressive hints or compare with the solution.

Run an attempt to unlock the solution.

Summary

  • println!, format!, vec!, and assert! are macros, not functions: that is why they carry !.
  • {} uses Display (human reading); {:?} uses Debug (debugging), and it is the only one that works with collections.
  • format! builds a String with the same syntax as println!, without printing anything.
  • vec! creates a Vec of variable size; unlike an array, it supports .push() and .pop().
  • assert!, assert_eq!, and assert_ne! panic with a formatted message when the check fails.
  • assert_eq!/assert_ne! require the type to implement Debug, to be able to print both sides if they fail.

Sources