Collections

    As core is, by definition, free of memory allocations these implementationsare not available there, but they can be found in the unstable alloc cratethat's shipped with the compiler.

    If you need collections, a heap allocated implementation is not your onlyoption. You can also use fixed capacity collections; one such implementationcan be found in the crate.

    In this section, we'll explore and compare these two implementations.

    The alloc crate is shipped with the standard Rust distribution. To import thecrate you can directly use it without declaring it as a dependency in yourCargo.toml file.

    To be able to use any collection you'll first need use the global_allocatorattribute to declare the global allocator your program will use. It's requiredthat the allocator you select implements the GlobalAlloc trait.

    For completeness and to keep this section as self-contained as possible we'llimplement a simple bump pointer allocator and use that as the global allocator.However, we strongly suggest you use a battle tested allocator from crates.ioin your program instead of this allocator.

    Apart from selecting a global allocator the user will also have to define howOut Of Memory (OOM) errors are handled using the unstablealloc_error_handler attribute.

    Once all that is in place, the user can finally use the collections in alloc.

    heapless requires no setup as its collections don't depend on a global memoryallocator. Just use its collections and proceed to instantiate them:

    You'll note two differences between these collections and the ones in .

    First, you have to declare upfront the capacity of the collection. heaplesscollections never reallocate and have fixed capacities; this capacity is part ofthe type signature of the collection. In this case we have declared that xshas a capacity of 8 elements that is the vector can, at most, hold 8 elements.This is indicated by the U8 (see ) in the type signature.

    Second, the push method, and many other methods, return a Result. Since theheapless collections have fixed capacity all operations that insert elementsinto the collection can potentially fail. The API reflects this problem byreturning a Result indicating whether the operation succeeded or not. Incontrast, alloc collections will reallocate themselves on the heap to increasetheir capacity.

    As of version v0.4.x all heapless collections store all their elements inline.This means that an operation like let x = heapless::Vec::new(); will allocatethe collection on the stack, but it's also possible to allocate the collectionon a static variable, or even on the heap (Box<Vec<, >>).

    Keep these in mind when choosing between heap allocated, relocatable collectionsand fixed capacity collections.

    With heap allocations Out Of Memory is always a possibility and can occur inany place where a collection may need to grow: for example, allalloc::Vec.push invocations can potentially generate an OOM condition. Thussome operations can implicitly fail. Some alloc collections exposetry_reserve methods that let you check for potential OOM conditions whengrowing the collection but you need be proactive about using them.

    If you exclusively use heapless collections and you don't use a memoryallocator for anything else then an OOM condition is impossible. Instead, you'llhave to deal with collections running out of capacity on a case by case basis.That is you'll have deal with all the s returned by methods likeVec.push.

    Reasoning about memory usage of heap allocated collections is hard because thecapacity of long lived collections can change at runtime. Some operations mayimplicitly reallocate the collection increasing its memory usage, and somecollections expose methods like shrinkto_fit that can potentially reduce thememory used by the collection — ultimately, it's up to the allocator to decidewhether to actually shrink the memory allocation or not. Additionally, theallocator may have to deal with memory fragmentation which can increase the_apparent memory usage.

    On the other hand if you exclusively use fixed capacity collections, storemost of them in static variables and set a maximum size for the call stackthen the linker will detect if you try to use more memory than what's physicallyavailable.

    Furthermore, fixed capacity collections allocated on the stack will be reportedby -Z emit-stack-sizes flag which means that tools that analyze stack usage(like ) will include them in their analysis.

    However, fixed capacity collections can not be shrunk which can result inlower load factors (the ratio between the size of the collection and itscapacity) than what relocatable collections can achieve.

    If are building time sensitive applications or hard real time applications thenyou care, maybe a lot, about the worst case execution time of the differentparts of your program.

    The alloc collections can reallocate so the WCET of operations that may growthe collection will also include the time it takes to reallocate the collection,which itself depends on the runtime capacity of the collection. This makes ithard to determine the WCET of, for example, the alloc::Vec.push operation asit depends on both the allocator being used and its runtime capacity.

    On the other hand fixed capacity collections never reallocate so all operationshave a predictable execution time. For example, heapless::Vec.push executes inconstant time.

    alloc requires setting up a global allocator whereas heapless does not.However, heapless requires you to pick the capacity of each collection thatyou instantiate.