面试题答案
一键面试struct Student {
name: String,
score: i32,
}
fn sort_students(students: &mut Vec<Student>) {
students.sort_by_key(|student| (-student.score, &student.name));
}
你可以使用以下方式测试这个函数:
fn main() {
let mut students = vec![
Student { name: "Alice".to_string(), score: 85 },
Student { name: "Bob".to_string(), score: 90 },
Student { name: "Charlie".to_string(), score: 85 },
];
sort_students(&mut students);
for student in students {
println!("Name: {}, Score: {}", student.name, student.score);
}
}