面试题答案
一键面试struct Point {
x: i32,
y: i32,
}
fn main() {
let mut points: Vec<Point> = vec![
Point { x: 2, y: 3 },
Point { x: 1, y: 4 },
Point { x: 2, y: 1 },
];
let compare_points: fn(&Point, &Point) -> std::cmp::Ordering = |a, b| {
if a.x != b.x {
a.x.cmp(&b.x)
} else {
a.y.cmp(&b.y)
}
};
points.sort_by(compare_points);
for point in points {
println!("x: {}, y: {}", point.x, point.y);
}
}