Rust Fundamentals · 10 XP

Printing values

println! takes a format string. Each {} is a placeholder that gets replaced by the next argument, using the value's human-friendly Display form.

let city = "Oslo";
let temp = 4;
println!("It is {} degrees in {}", temp, city);

A few other tools you will use constantly:

  • {:?} prints the debug form. For text this adds quotes: "Oslo".
  • Named arguments: write {who} in the format string and pass who = city after it (or just have a variable called who in scope).
  • {:.2} limits a float to 2 digits after the decimal point.
  • {:>6} right-aligns in a field 6 characters wide ({:<6} aligns left, and {:^6} centres).
println!("[{:>6}] [{:<6}] [{:.1}]", 42, 42, 2.55);
// [    42] [42    ] [2.5]

Your task

main already defines name, age and pi. Add println! calls so the program prints exactly these five lines:

Ferris is 7 years old
Debug: "Ferris"
Hello, Ferris!
pi is about 3.14
[    42]

Use {} for the first line, {:?} for the second, a named argument for the third, {:.2} for the fourth, and a width of 6 for the last line.

Next lesson →

Sign in to run your code. It's free.