Sub topic 3
Positional, named, and inline arguments
This chapter assumes you can write a main function, run it with cargo run, and know that println! takes a format string with {} placeholders. Here you will learn the three ways a placeholder can find its value.
Three ways to supply a value
A placeholder is a pair of braces inside the format string. Rust has to match each one to a value. It offers three ways to do that.
| Form | Example | Value comes from |
|---|---|---|
| Implicit position | `{}` | The next unused argument, left to right |
| Explicit position | `{0}`, `{1}` | The argument at that index, counting from 0 |
| Named | `{n}` | A `n = ...` argument, or a variable called `n` in scope |
fn main() {
let name = "Ferris";
let age = 7;
println!("Hello, {}!", name);
println!("{name} is {age} years old");
println!("{0} {1} {0}", "a", "b");
println!("{n} squared is {sq}", n = 5, sq = 5 * 5);
}Hello, Ferris!
Ferris is 7 years old
a b a
5 squared is 25Implicit and explicit positions
Plain {} placeholders consume arguments in order. The first {} takes argument 0, the second takes argument 1, and so on.
An explicit index such as {0} lets you reuse a value or change the order. It does not move the counter used by plain {}. That is a common surprise:
fn main() {
println!("{} {0} {}", "a", "b");
}This prints a a b. The first {} takes "a", {0} takes "a" again, and the second {} takes "b". The explicit index did not advance the counter.
Named arguments
Write name = value after the positional arguments to give a value a label. The label makes long format strings easier to read, and you can use it more than once. Named arguments must come after all positional ones.
Inline captured arguments
If the name inside the braces is not given as an argument, Rust looks for a variable with that name in the surrounding scope. {name} above reads the local variable name. This is the shortest form and the usual choice for simple values.
Format options still work with inline names: {pi:.2} prints the variable pi with two decimal places.
The braces can hold only a plain identifier. Expressions and field accesses are not allowed:
struct Point {
x: i32,
}
fn main() {
let p = Point { x: 1 };
// println!("{p.x}"); // error: not a plain identifier
println!("{}", p.x); // pass the expression as an argument
println!("{x}", x = p.x); // or give it a name
}Common errors
- Unused argument.
println!("{}", 1, 2)fails to compile. Every argument must be used. - Index out of range.
println!("{1}", "a")fails because there is no argument 1. - Missing name.
{count}with nocountargument and no variable in scope is a compile error. - Wrong order. A positional argument after a named one is rejected.
All of these are caught at compile time, so they never reach a running program.
Exercise
What does println!("{} {1} {}", "x", "y"); print? Then rewrite it using a named argument so the code prints y x y and still uses both values.
Show answer
It prints x y y. The first {} takes argument 0 ("x"), {1} takes "y", and the second {} takes argument 1 ("y"). One rewrite that prints y x y is println!("{b} {a} {b}", a = "x", b = "y");.