Skip to content

Commit eed678a

Browse files
jyasskingraydon
authored andcommitted
---
yaml --- r: 278 b: refs/heads/master c: 765a2b3 h: refs/heads/master v: v3
1 parent 76cf915 commit eed678a

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: c866672a994267c165bef960d90b0fa8f9677b22
2+
refs/heads/master: 765a2b3ecffb68a18849de6db54a680a1fd6eee4

trunk/src/lib/_vec.rs

Lines changed: 15 additions & 0 deletions
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] {

trunk/src/test/run-pass/vec-lib.rs

Lines changed: 12 additions & 1 deletion
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)