Many standards for safety-critical embedded code (which seems to be one of Rust's target markets) prevent the use of dynamic memory allocation outside of initialization.
See Why shouldn't we have dynamic allocated memory with different size in embedded system - Stack Overflow and Justifiably taboo: Avoiding malloc-free APIs in military/aerospace embedded code - Military Embedded Systems for reasoning.
The main points against dynamic memory allocation appear to be:
- Creates the potential for memory leaks
- Difficult to verify maximum potential memory usage
- Heap access is non-deterministic, making it problematic for hard real-time systems
Rust appears to avoid the first issue with borrow-checking. However, the last two issues appear to still be valid.
I'm still new to Rust so apologies if I'm missing it (my literal first day learning it), but Rust doesn't appear to offer a relatively simple construct for mutable fixed-size strings like in C/C++? For instance, how could I do something like this in Rust?
const size_t FIXED_STR_SIZE_BYTES = 1024;
char testStr[FIXED_STR_SIZE_BYTES];
char str[10] = "hello";
strncpy(testStr, str, FIXED_STR_SIZE_BYTES);
Thanks!