Skip to content

Formatting values

f"{x}" puts a value in a string; {{ and }} are literal braces. A spec after : controls the layout: [[fill]align][0][width][.precision][type]. f"{price:>8.2}", f"{n:05}", f"{id:x}", f"{name:-^20}". Align is <, > or ^ (numbers default to the right, text to the left); 0 pads numbers with zeros after the sign; .N is decimals for floats and a maximum length for text; the type is x X b o for integers (negative ones show their two’s complement) and f e for floats. Widths count code points. A bad spec is a compile error. The expression inside {} can hold string and character literals: f"{m.get("key").unwrap()}".