面试题答案
一键面试fn clone_and_print<T: std::fmt::Debug + Clone>(arg: T) {
let cloned_arg = arg.clone();
println!("Cloned value: {:?}", cloned_arg);
}
在上述代码中,<T: std::fmt::Debug + Clone>
这部分指定了特型约束。T
是泛型参数,: std::fmt::Debug + Clone
表示 T
类型必须实现 Debug
和 Clone
这两个特型。Debug
特型用于格式化输出调试信息,Clone
特型用于克隆对象。这样就能在函数内部对传入的参数进行克隆并打印克隆后的结果。