Add a _vec.slice function that'll hold us over until .(a,b) syntax is
implemented. This could actually replace .(a,b) syntax if the language grows optional function parameters.
This commit is contained in:
parent
c866672a99
commit
765a2b3ecf
@ -48,6 +48,21 @@ fn buf[T](vec[T] v) -> vbuf {
|
||||
ret rustrt.vec_buf[T](v);
|
||||
}
|
||||
|
||||
// Returns elements from [start..end) from v.
|
||||
fn slice[T](vec[T] v, int start, int end) -> vec[T] {
|
||||
check(0 <= start);
|
||||
check(start <= end);
|
||||
// FIXME #108: This doesn't work yet.
|
||||
//check(end <= int(len[T](v)));
|
||||
auto result = alloc[T](uint(end - start));
|
||||
let mutable int i = start;
|
||||
while (i < end) {
|
||||
result += vec(v.(i));
|
||||
i += 1;
|
||||
}
|
||||
ret result;
|
||||
}
|
||||
|
||||
// Ought to take mutable &vec[T] v and just mutate it instead of copy
|
||||
// and return. Blocking on issue #89 for this.
|
||||
fn grow[T](mutable vec[T] v, int n, T initval) -> vec[T] {
|
||||
|
@ -24,7 +24,18 @@ fn test_init_fn() {
|
||||
check (v.(4) == uint(4));
|
||||
}
|
||||
|
||||
fn test_slice() {
|
||||
let vec[int] v = vec(1,2,3,4,5);
|
||||
auto v2 = std._vec.slice[int](v, 2, 4);
|
||||
// FIXME #108: Can't call templated function twice in the same
|
||||
// program, at the moment.
|
||||
//check (std._vec.len[int](v2) == uint(2));
|
||||
check (v2.(0) == 3);
|
||||
check (v2.(1) == 4);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
test_init_elt();
|
||||
//XFAIL: test_init_fn(); // Segfaults.
|
||||
}
|
||||
test_slice();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user