面试题答案
一键面试use std::fmt::Debug;
use std::ops::Sub;
use std::sqrt;
// 定义泛型结构体Point
struct Point<T>
where
T: Debug + Sub<Output = T> + Copy + std::ops::Mul<Output = T> + std::convert::Into<f64>,
{
x: T,
y: T,
}
// 实现distance函数
fn distance<T>(p1: Point<T>, p2: Point<T>) -> f64
where
T: Debug + Sub<Output = T> + Copy + std::ops::Mul<Output = T> + std::convert::Into<f64>,
{
let dx = (p2.x - p1.x).into();
let dy = (p2.y - p1.y).into();
sqrt(dx * dx + dy * dy)
}