Skip to content

Commit 765a2b3

Browse files
jyasskingraydon
authored andcommitted
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.
1 parent c866672 commit 765a2b3

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

Diff for: src/lib/_vec.rs

+15
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ fn buf[T](vec[T] v) -> vbuf {
4848
ret rustrt.vec_buf[T](v);
4949
}
5050

51+
// Returns elements from [start..end) from v.
52+
fn slice[T](vec[T] v, int start, int end) -> vec[T] {
53+
check(0 <= start);
54+
check(start <= end);
55+
// FIXME #108: This doesn't work yet.
56+
//check(end <= int(len[T](v)));
57+
auto result = alloc[T](uint(end - start));
58+
let mutable int i = start;
59+
while (i < end) {
60+
result += vec(v.(i));
61+
i += 1;
62+
}
63+
ret result;
64+
}
65+
5166
// Ought to take mutable &vec[T] v and just mutate it instead of copy
5267
// and return. Blocking on issue #89 for this.
5368
fn grow[T](mutable vec[T] v, int n, T initval) -> vec[T] {

Diff for: src/test/run-pass/vec-lib.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,18 @@ fn test_init_fn() {
2424
check (v.(4) == uint(4));
2525
}
2626

27+
fn test_slice() {
28+
let vec[int] v = vec(1,2,3,4,5);
29+
auto v2 = std._vec.slice[int](v, 2, 4);
30+
// FIXME #108: Can't call templated function twice in the same
31+
// program, at the moment.
32+
//check (std._vec.len[int](v2) == uint(2));
33+
check (v2.(0) == 3);
34+
check (v2.(1) == 4);
35+
}
36+
2737
fn main() {
2838
test_init_elt();
2939
//XFAIL: test_init_fn(); // Segfaults.
30-
}
40+
test_slice();
41+
}

0 commit comments

Comments
 (0)