面试题答案
一键面试// 定义函数别名
type Handler<T> = fn(T) -> T;
// 泛型函数,接受泛型类型参数T和处理函数handler
fn process_data<T>(data: T, handler: Handler<T>) -> T {
handler(data)
}
// 定义两个不同处理逻辑的函数
fn double(x: i32) -> i32 {
x * 2
}
fn square(x: f64) -> f64 {
x * x
}
fn main() {
let result1 = process_data(5, double);
let result2 = process_data(3.5, square);
println!("Result 1: {}", result1);
println!("Result 2: {}", result2);
}