I have the following issue: In the get_new_experiences
method I want to return a Vec while also emptying it.
struct ExperienceBuffer {
experiences: Vec<Experience>,
}
impl ExperienceBuffer {
pub fn new(capacity: usize) -> ExperienceBuffer {
return ExperienceBuffer {
experiences: Vec::with_capacity(capacity),
};
}
pub fn put_experience(&mut self, exp: Experience) {
self.experiences.push(exp);
}
pub fn get_new_experiences(&mut self) -> Vec<Experience> {
let mut result = Vec::new();
swap(&mut self.experiences, &mut result);
return result;
}
}
Is this the correct way to do this?