Rust Formatting
ExampleRust
fn main() {
let name = "Ada";
let pi = 3.14159265;
println!("{name} has {pi:.2}");
println!("[{:>8}] [{:<8}] [{:^8}]", "right", "left", "mid");
println!("[{:08.3}] [{:+}]", pi, 5);
println!("{:b} {:o} {:x} {:X}", 10, 10, 255, 255);
println!("{0} {1} {0}", "a", "b");
println!("{:?} {:?}", "text", Some(3));
let width = 6;
println!("[{:>width$}]", 42);
println!("{}", format!("{:05}", 42));
eprintln!("this goes to stderr");
}Output
Ada has 3.14 [ right] [left ] [ mid ] [0003.142] [+5] 1010 12 ff FF a b a "text" Some(3) [ 42] 00042
Quick check
Which format spec rounds a float to two decimals?
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.