Provides a variable-length array (VLA), also called variable-sized or runtime-sized.
It is an array data structure whose length is determined at run time (instead of at compile time).
The main purpose of VLAs is to simplify programming of numerical algorithms.
Unlike a dynamic array, a VLA cannot change its size, which is determined once, at the time of creation. But they may be more comfortable in use than static arrays, whose size must be known at compile time.
What is more, the VLA, provided by this crate in some cases is more
efficient than std::vec::Vec
. That's because of some optimizations and closeness to
C++ arrays, allocated by malloc
. That's why some methods are unsafe.
```rust use runtimesizedarray::Array;
let arr1: Array
let mut vec = vec![1,2,3];
let ptr = vec.asmutptr();
let size = vec.len();
let arr2: Array
let arr3: Array
```
```rust use runtimesizedarray::Array;
let mut array : Array
// mutable iterator for item in array.iter_mut() { *item += 1; }
// immutable iterator for item in array.iter() { println!("{item}"); }
// immutable iterator for item in &array { println!("{item}"); }
// again mutable iterator for item in &mut array { *item *= 0; } ```
Safe access:
```rust use runtimesizedarray::Array;
let mut arr: Array
// immutable access asserteq!(arr.tryget(1), Some(&2)); asserteq!(arr.tryget(10), None);
// mutable access *arr.trygetmut(1).unwrap() = 5; asserteq!(arr.tryget_mut(10), None);
// alternative mutable access asserteq!(tryset(1, 5), Some(())); asserteq!(arr.tryset(10, 4), None);
// by brackets arr[0] = 17; assert_eq!(arr[0], 17);
```
Unsafe access:
```rust use runtimesizedarray::Array;
let mut arr: Array
// immutable access unsafe { assert_eq!(arr.get(1), &2) } // arr.get(10) - undefined behaviour
unsafe { *arr.getmut(1) = 2; } // *arr.getmut(10) == 4; - undefined behaviour
// alternative mutable access unsafe { arr.set(1, 5); } // arr.set(10, 4); - undefined behaviour
unsafe { arr.get_mut_ptr(0) = 10; assert_eq!(arr.get_ptr(0), 10) }
```