tinyvec

Sometimes you want something which can be resized like a Vec, but without heap allocation. tinyvec provides this: a vector backed by an array or slice, which could be statically allocated or on the stack, which keeps track of how many elements are used and panics if you try to use more than are allocated.

  1. use tinyvec::{array_vec, ArrayVec};
  2. fn main() {
  3.     let mut numbers: ArrayVec<[u32; 5]> = array_vec!(42, 66);
  4.     println!("{numbers:?}");
  5.     numbers.push(7);
  6.     println!("{numbers:?}");
  7.     numbers.remove(1);
  8.     println!("{numbers:?}");
  9. }
  • tinyvec requires that the element type implement Default for initialisation.
  • The Rust Playground includes tinyvec, so this example will run fine inline.