Rs
Module 5Vectors & IteratorsLesson 1 of 820 min

Vec Basics

Growable arrays: push, indexing, capacity, and ownership.

Vec<T> is your workhorse

rust
fn main() {
    let mut xs = vec![1.0, 2.0, 3.0];
    xs.push(4.0);
    println!("{} {}", xs[0], xs.len());
}
Output
1 4

with_capacity

Preallocate when you know path length:

rust
let mut path = Vec::with_capacity(252);

Finance uses

Trade lists, return series, MC paths, cashflow schedules.

Challenge

Build a Vec of 10 synthetic prices starting at 100 with +0.5 each step.

Tips
  • vec! macro for initial values
  • last() returns Option<&T>
  • Prefer with_capacity for hot paths

Try it live

Vec BasicsRust 2021
Loading editor…
Output
Press Run to compile and execute.