MCPcopy Create free account
hub / github.com/bedroombuilds/python2rust / main

Function main

03_tuples_arrays/rust/vectors.rs:3–39  ·  view source on GitHub ↗

demo of vectors

()

Source from the content-addressed store, hash-verified

1//! demo of vectors
2
3fn main() {
4 let mut xs = vec![1_i32, 2, 3];
5 println!("Initial vector: {:?}", xs);
6
7 println!("Push 4 into the vector");
8 xs.push(4);
9 println!("Vector: {:?}", xs);
10
11 println!("Vector length: {}", xs.len());
12
13 println!("Second element: {}", xs[1]);
14
15 println!("Pop last element: {:?}", xs.pop());
16
17 println!("Fourth element: {}", xs[3]);
18
19 println!("Contents of xs:");
20 for x in xs {
21 // borrow or don't use into_iter()
22 println!("> {}", x);
23 }
24
25 /*
26 for (i, x) in xs.iter().enumerate() {
27 println!("In position {} we have value {}", i, x);
28 }
29
30 for x in xs.iter_mut() {
31 *x *= 3;
32 }
33 let xs: Vec<i32> = xs.iter().map(|x| x * 3).collect();
34 println!("Updated vector: {:?}", xs);
35
36 let collected_iterator: Vec<i32> = (0..10).collect();
37 println!("Collected (0..10) into: {:?}", collected_iterator);
38 */
39}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected