Pybites Logo Rust Platform

Vec Operations

Medium +3 pts

In Python, lists are your go-to collection:

items = [1, 2, 3] items.append(4) items.extend([5, 6]) first = items.pop(0) items.insert(0, first)

Rust's Vec<T> is similar but with explicit types and ownership semantics.

Creating vectors

let mut v: Vec<i32> = Vec::new(); let v = vec![1, 2, 3]; // macro for convenience

Adding elements

let mut v = vec![1, 2, 3]; v.push(4); // add to end v.extend([5, 6]); // add multiple v.insert(0, 0); // insert at index

Login to see the full exercise.