MCPcopy Create free account
hub / github.com/douchuan/algorithm / vec_random_access

Function vec_random_access

tests/playground.rs:2–26  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

1#[test]
2fn vec_random_access() {
3 // Allocate vector big enough for 4 elements.
4 let size = 4;
5 let mut x: Vec<usize> = Vec::with_capacity(size);
6 let x_ptr = x.as_mut_ptr();
7
8 unsafe {
9 for v in 0..4 {
10 *x_ptr.add(v) = v;
11 }
12 x.set_len(size);
13 }
14 assert_eq!(&*x, &[0, 1, 2, 3]);
15
16 // 扩充
17 x.reserve(4);
18 let x_ptr = x.as_mut_ptr();
19 unsafe {
20 for v in 4..8 {
21 *x_ptr.add(v) = v;
22 }
23 x.set_len(8)
24 }
25 assert_eq!(&*x, &[0, 1, 2, 3, 4, 5, 6, 7]);
26}

Callers

nothing calls this directly

Calls 1

addMethod · 0.80

Tested by

no test coverage detected