Skip to main content

try_vec

Macro try_vec 

Source
macro_rules! try_vec {
    ($($x:expr),* $(,)?) => { ... };
    ($elem:expr; $n:expr) => { ... };
}
Expand description

Macro to construct a fallible Vector.

This macro is analogous to the standard vec! macro but returns an Option<Vector<T>> to handle allocation failures gracefully.

§Returns

  • Some(Vector<T>) on successful allocation.
  • None if allocation fails.

§Examples

use fbl::try_vec;

// Constructing a vector with a list of elements:
let v = try_vec![1, 2, 3].expect("Allocation failed");
assert_eq!(v.len(), 3);

// Constructing a vector with a repeated element:
let v2 = try_vec![0; 5].expect("Allocation failed");
assert_eq!(v2.len(), 5);
assert_eq!(v2[0], 0);