面试题答案
一键面试struct Container<T> {
data: Vec<T>,
}
impl<T> Container<T> {
fn new(data: Vec<T>) -> Self {
Container { data }
}
fn map<U, F>(self, f: F) -> Container<U>
where
F: FnMut(T) -> U,
{
let mut new_data = Vec::with_capacity(self.data.len());
for item in self.data {
new_data.push(f(item));
}
Container { data: new_data }
}
}