Skip to content

Instantly share code, notes, and snippets.

@derekchiang
Created January 5, 2014 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derekchiang/8268780 to your computer and use it in GitHub Desktop.
Save derekchiang/8268780 to your computer and use it in GitHub Desktop.
A Rust function for splitting an owned vector into two owned vectors.
fn split_owned_vec<T>(mut v: ~[T], index: uint) -> (~[T], ~[T]) {
assert!(index <= v.len());
let new_len = v.len() - index;
let mut new_v = vec::with_capacity(v.len() - index);
unsafe {
ptr::copy_nonoverlapping_memory(new_v.as_mut_ptr(), v.as_ptr().offset(index as int), new_len);
v.set_len(index);
new_v.set_len(new_len);
}
(v, new_v)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment