I agree with David Grayson, there's no obvious need to recreate the RwLock
. Assuming you need the vector after filling it up, use mem::replace
to switch out the Vec
:
use std::sync::RwLock;
use std::mem;
fn main() {
let locked = RwLock::new(Vec::::new());
let mut writer = locked.write().unwrap();
for v in 0..100 {
if writer.len() > 4 {
let old_vec = mem::replace(&mut *writer, Vec::new());
}
writer.push(v);
}
}
If you don't need the Vec
, then just call Vec::clear.