面试题答案
一键面试use std::fmt::Debug;
// 泛型函数print_info
fn print_info<T: Debug>(t: &T) {
println!("{:?}", t);
}
// Point结构体
struct Point {
x: i32,
y: i32,
}
// 为Point结构体实现泛型方法distance
impl Point {
fn distance(&self, other: &impl Copy + Sized + PartialEq<Self> + Into<Point>) -> f64 {
let other: Point = other.into();
((self.x - other.x) as f64).powi(2) + ((self.y - other.y) as f64).powi(2)
.sqrt()
}
}
impl Into<Point> for Point {
fn into(self) -> Point {
self
}
}