daily pastebin goal
28%
SHARE
TWEET

[Rust] String formatting

cwchen Aug 26th, 2017 (edited) 122 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. fn main() {
  2.     // Hexadeicmal number
  3.     assert_eq!(format!("0x{:X}", 255), "0xFF");
  4.  
  5.     // Octal number
  6.     assert_eq!(format!("0o{:o}", 127), "0o177");
  7.  
  8.     // Decimal with specific precision
  9.     assert_eq!(format!("{:.2}", 3.14159), "3.14");
  10.  
  11.     // Formatted string for debug purpose
  12.     assert_eq!(format!("{:?}", vec![1, 2, 3]), "[1, 2, 3]");
  13.  
  14.     // Formatted string with specific position
  15.     assert_eq!(format!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob"),
  16.                "Alice, this is Bob. Bob, this is Alice");
  17.  
  18.     // Formatted string with specific name
  19.     assert_eq!(format!("{subject} {verb} {object}", object = "the lazy dog",
  20.                        verb = "jumps over", subject = "The swift fox"),
  21.                "The swift fox jumps over the lazy dog");
  22. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top