Skip to content

Instantly share code, notes, and snippets.

@jld
Created August 28, 2012 19:00
Show Gist options
  • Save jld/3502438 to your computer and use it in GitHub Desktop.
Save jld/3502438 to your computer and use it in GitHub Desktop.
Rust vector shortening
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