Created
August 28, 2012 19:00
-
-
Save jld/3502438 to your computer and use it in GitHub Desktop.
Rust vector shortening
This file contains 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
import vec::*; | |
fn truncate<T>(&v: ~[const T], newlen: uint) { | |
let oldlen = len(v); | |
assert(newlen <= oldlen); | |
unsafe { | |
for uint::range(newlen, oldlen) |i| { | |
let _dropped <- *ptr::mut_addr_of(v[i]); | |
} | |
unsafe::set_len(v, newlen); | |
} | |
} | |
#[cfg(test)] | |
use std; | |
#[test] | |
fn test_truncate() { | |
let mut v = ~[@6,@5,@4]; | |
truncate(v, 1); | |
assert(v.len() == 1); | |
assert(*(v[0]) == 6); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment