Step 6 of 6
Inline format arguments and {:?}
Names inside the braces
So far you've passed values after the format string: println!("{} scored {}", player, score). When a value is a plain variable, you can write its name inside the braces instead:
fn main() {
let player = "Ferris";
let score = 120;
println!("{player} scored {score} points");
println!("{} scored {} points", player, score);
}
Ferris scored 120 points
Ferris scored 120 points
These inline format arguments read like the final text, and you can't mix up the order. Both forms are correct Rust. Most new code uses the inline form, and clippy has an optional lint that suggests it (uninlined_format_args, part of its stricter pedantic group). You can mix the two in one call: println!("{player} has {} lives", 2 + 1).
Only a plain variable name works inside the braces, not a calculation:
fn main() {
let score = 120;
println!("next: {score + 10}");
}
error: invalid format string: the `+` sign flag must appear after `:` in a format string
--> src/main.rs:3:28
|
3 | println!("next: {score + 10}");
| ^ expected `:` before `+` sign flag in format string
|
= note: `+` comes after `:`, try `{:+}` instead of `{+}`
The message is misleading here: rustc reads the + as a formatting flag ({:+} prints a + sign on positive numbers), so it suggests moving it. That isn't what you meant. Put the calculation after the format string, println!("next: {}", score + 10), or store it in a variable first.
Debug output with {:?}
{} uses a value's Display format: the clean form meant for users. {:?} uses its Debug format: the form meant for programmers, which shows exactly what the value is. For text, Debug adds the quotes and shows escapes like \n, which makes invisible problems visible:
fn main() {
let name = " Ferris ";
let message = "line one\nline two";
println!("[{}]", name);
println!("{:?}", name);
println!("{message:?}");
}
[ Ferris ]
" Ferris "
"line one\nline two"
{message:?} combines both ideas: the variable's name, then :?. For integers, Debug and Display look the same. Debug becomes essential later: lists, your own data types and errors are printed with {:?}, and many types have no {} format at all.
At work, {:?} is how you look inside values while debugging. Messages meant for users should use {}.
Common mistakes
- A calculation inside the braces (the error above).
- A typo in the name:
{scroe}giveserror[E0425]: cannot find value `scroe` in this scope, the same error as any unknown variable. {?}or{?:}: it's a colon, then a question mark:{:?}.
Your task
Rewrite the println! to use inline format arguments ({name}, {level} and {hp} inside the string, nothing after it). The output stays Ferris is level 7 with 92 hp.
Challenge 2
Fill in the blank so the program prints the password in Debug format, which shows the quotes and the hidden trailing space: "hunter2 ".
Challenge 3
Print a two-line status report from the variables in the starter code. Use inline format arguments on both lines, and Debug format for status so its trailing space shows:
server eu-west has 1520 users
status: "degraded "