Fmt Structs
Formatting types
Formatter
Section titled “Formatter”use std::fmt::{self, Formatter};
struct Point { x: f64, y: f64,}
impl fmt::Display for Point { fn fmt(&self, f: &mut Formatter) -> fmt::Result { // Access formatter options if f.alternate() { // # flag write!(f, "Point {{\n x: {},\n y: {}\n}}", self.x, self.y) } else if let Some(precision) = f.precision() { write!(f, "({:.prec$}, {:.prec$})", self.x, self.y, prec = precision) } else { write!(f, "({}, {})", self.x, self.y) } }}
impl fmt::Debug for Point { fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.debug_struct("Point") .field("x", &self.x) .field("y", &self.y) .finish() }}
fn main() { let p = Point { x: 3.14159, y: 2.71828 };
// Different format options println!("Default: {}", p); println!("Alternate: {:#}", p); println!("Precision: {:.2}", p); println!("Debug: {:?}", p); println!("Pretty Debug: {:#?}", p);}เข้าสู่ระบบเพื่อดูเนื้อหาเต็ม
ยืนยันตัวตนด้วยบัญชี Google เพื่อปลดล็อกบทความทั้งหมด
Login with Google