Rust memory evaluation

Is there any evaluation or test about the memory usage of Rust compared with C?
Memory is sensitive in embedded device, I want to know apperance of Rust in this respect.

You're in full control of memory allocations, so how much Rust allocates is entirely up to you. Rust in no_std mode can run without any memory allocator at all, so the amount of heap-allocated memory can be as low as zero.

Only differences could come from coding style.

For example, generic code is monomorphised (sort-of like C macro-genreated code). This may make code smaller when functions are optimized for the exact types they're used with, but OTOH can cause code bloat if you overuse it. See cargo-bloat to check.

To get struct privacy in C you'd usually return pointers to heap-allocated opaque types. Rust has first-class struct privacy and borrow checking that makes on-stack values safe to use, so it tends to store more things on stack instead.

Rust likes usize a lot. On 32-bit platforms it doesn't matter, but on 64-bit platforms data structures will more often contain 64-bit values than in a C program that uses int everywhere.

Rust slices/str and dyn Trait objects are fat pointers (2× usize). It's a different trade-off than nul-terminated strings, size-less char*, and double-indirection vtables.

5 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.