//对没被调用的函数不报错
#![ allow(dead_code)]
use std::fmt;
#[derive(Debug)]
struct Point2D{
x:f64,
y:f64,
}
impl fmt::Display for Point2D{
fn fmt(&self,f: &mut fmt::Formatter) -> fmt::Result{
write!(f,"x:{}, y:{}",self.x,self.y)
}
}
fn main(){
let my_Point_2D = Point2D{x:22f64,y:23f64};
println!("Debug: {:#?}",my_Point_2D);
println!("Display: {}",my_Point_2D);
}
//输出
Debug: Point2D {
x: 22.0,
y: 23.0,
}
Display: x:22, y:23
Rust 为自定义的结构体实现Debug与Display
最新推荐文章于 2024-12-18 08:00:00 发布