我有以下Rust代码:
#[derive(Debug)]
struct Point<T, U> {
x: T,
y: U
}
impl<T: std::fmt::Display, U: std::fmt::Display> Point<T, U> {
fn print(&self) {
println!("{} {}", &self.x, &self.y );
}
}
impl<X1, Y1> Point<X1, Y1> {
fn mixup<X2, Y2>(self, other: Point<X2, Y2>) -> Point<X1, Y2> {
Point {
x: self.x,
y: other.y,
}
}
}
fn main() {
let integer = Point {x: 1, y:2};
let float = Point{x: 2.0, y:3.0};
let floaty = integer.mixup(float);
println!("{:?}", floaty); // output: Point { x: 1, y: 3.0 } - OK
floaty.print(); // output: 1 3 - NOT OK
}
为什么
floaty.print()
胁迫
3.0
到
3
? 如何
print
方法实现了吗?