You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fnmain(){let s = "abc".to_string();let t = 123456789u64;println!("{} {}", s, t);println!("{1} {0}", s, t);println!("{s} {}", t);println!("{} {u}", t, u = s);}
Formatting width, precision and alignment
fnmain(){let s = "abc".to_string();let f = 123.456789f64;println!("{}", f);println!("{:20}", f);println!("{:>20}", f);println!("{:<20}", f);println!("{:^20}", f);println!("{:20}", s);println!("{:>20}", s);println!("{:<20}", s);println!("{:^20}", s);println!("{:20.4}", f);println!("{:<20.4}", f);}
Fill and Sign
fnmain(){let f = 123.456789f64;let i = 123456789u64;println!("{:*>20}", f);// Fill with starsprintln!("{:*>+20}", f);// Always print the signprintln!("{:*>#20x}", i);// Alternative formprintln!("{:#?}", vec![17, 18, 19]);// Alternative form}
Formatting traits
fnmain(){let s = "abc".to_string();let f = 123.456789f64;let i = 987654321u64;println!("{}", i);println!("{:b}", i);// binaryprintln!("{:20o}", i);// octalprintln!("{:20x}", i);// hexprintln!("{:20X}", i);// Hex (capitalized)println!("{:20e}", f);// scientificprintln!("{:20E}", f);// scientific (capitalized)println!("{:p}", &s);// pointer}
Debug Formatting
#[derive(Debug)]structPos{x:f64,y:f64,}fnmain(){let s = "abc".to_string();let f = 123.456789f64;let i = 987654321u64;let p = Pos{x:17.1,y: -3.2};println!("{:?}", s);println!("{:?}", f);println!("{:?}", i);println!("{:?}", p);println!("{:?}", vec![1u32, 2u32, 3u32]);println!("{} {}", p.x, p.y);}
Putting it all together
fnmain(){let f = 123.456789f64;let i = 987654321u64;let s = "abc".to_string();eprintln!("}} {f:-^+20.4} {{");// format f, fill with -, with signprintln!("}} {v:*^#30x} {{", v=i);// format i, fill with *, 0x prefixprintln!("}} {0:?>20p} {{", &s);// format &s, fill with ?, as pointerprintln!("}} {:width$.4} {{", f, width = 30);// runtime specified width}