Created
January 5, 2014 14:23
-
-
Save derekchiang/8268780 to your computer and use it in GitHub Desktop.
A Rust function for splitting an owned vector into two owned vectors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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