Macros

Macros are expanded into Rust code during compilation, and can take a variable number of arguments. They are distinguished by a ! at the end. The Rust standard library includes an assortment of useful macros.

  • println!(format, ..) prints a line to standard output, applying formatting described in std::fmt.
  • format!(format, ..) works just like println! but returns the result as a string.
  • dbg!(expression) logs the value of the expression and returns it.
  • todo!() marks a bit of code as not-yet-implemented. If executed, it will panic.
  • unreachable!() marks a bit of code as unreachable. If executed, it will panic.
  1. fn factorial(n: u32) -> u32 {
  2.     let mut product = 1;
  3.     for i in 1..=n {
  4.         product *= dbg!(i);
  5.     }
  6.     product
  7. }
  8. fn fizzbuzz(n: u32) -> u32 {
  9.     todo!()
  10. }
  11. fn main() {
  12.     let n = 4;
  13.     println!("{n}! = {}", factorial(n));
  14. }

This slide should take about 2 minutes.

The takeaway from this section is that these common conveniences exist, and how to use them. Why they are defined as macros, and what they expand to, is not especially critical.

The course does not cover defining macros, but a later section will describe use of derive macros.