Format strings in Rust
The formatting mini-language: width, precision, alignment, debug output.
fn main() {
let name = "polars";
let rate = 91.5_f64;
let rows = vec![1, 2, 3];
println!("{name:>10} | {rate:6.2}");
println!("{:<10} | {:06.2}", "numpy", 8.0);
println!("{rows:?}");
println!("{:#?}", (1, "two"));
println!("{:08b} {:#x}", 5, 255);
println!("{}%", (rate * 10.0).round() / 10.0);
}
How it works
{:>8}right-aligns in eight columns,{:<}left-aligns.{:.2}fixes the decimals,{:?}asks for the debug form.- A name inside the braces reads the variable directly.
Keywords and builtins used here
f64fnletmain
The run, in numbers
- Lines
- 12
- Characters to type
- 297
- Tokens
- 103
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 69 seconds.
Step 1 of 3 in Text, step 27 of 39 in Language basics.