面试题答案
一键面试macro_rules! generate_struct_with_method {
($name:ident) => {
pub struct $name<T, U> {
data1: T,
data2: U,
}
impl<T, U> $name<T, U> {
pub fn combine(&self) -> String
where
T: std::fmt::Display,
U: std::fmt::Display,
{
format!("{}{}", self.data1, self.data2)
}
}
};
}
// 使用示例
generate_struct_with_method!(MyStruct);
fn main() {
let instance = MyStruct {
data1: 10,
data2: "hello",
};
let result = instance.combine();
println!("{}", result);
}