这里是一段Rust 代码
`#[derive(Debug)]
struct Point {
x: f32,
y: f32,
}
#[derive(Debug)]
struct Rectangle {
p1: Point,
p2: Point,
}
impl Rectangle {
fn rect_area(&self) -> f32 {
let width = (self.p1.x - self.p2.x).abs();
let height = (self.p1.y - self.p2.y).abs();
println!(
"nwidth is{} - {} = {}, height is{}-{} = {}",
self.p1.x, self.p2.x, &width, self.p1.y, self.p2.y, &height
);
width * height
}
}
fn square(p: Point, num: f32) -> Rectangle {
let p2 = Point {
x: p.x + num,
y: p.y + num,
};
Rectangle { p1: p, p2 }
}
pub fn struct_fn() {
let p = Point { x: 3.3, y: 4.4 };
let new_r = square(p, 1.1);
print!("{:?}", &new_r);
print!("{:?}", new_r.rect_area())
}`
结果:
Rectangle { p1: Point { x: 3.3, y: 4.4 }, p2: Point { x: 4.4, y: 5.5 } }
width is3.3 - 4.4 = 1.1000001, height is4.4-5.5 = 1.0999999
1.21
这里打印出来的长宽 都应该是1.1, 然后乘积又是准确的
嗯...
不是很明白这个..
有大佬能解答这个问题吗?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…