RUST lang and ownership

One of the most promising candidates to replacing C as the default language for e.g., embedded programs is RUST lang. One of the most important pillar of RUST is the memory management, because it comes without a (dynamic) garbage collector. The garbage collector approach is replaced by and so called ownership approach. Purpose of ownership is to manage heap, not the stack. Heap is the free floating memory assigned to an executed program.

The ownership concept assigns an owner to a memory address. As soon as the owner is out of the program execution scope, the memory gets free. A scope is e.g., determined between the following two brackets { and }. In RUST we do not have memory leaks coming from misused alloc and free calls.

So far so good, but the common beginners mistake happen with quite common assignments like a=b. If b is a simple type (fix sized) and not an Object it works as expected; b will give its value to a. Object types such as String just copy the pointer and all related meta-data, not the value itself. So if an and b go out of scope, RUST would free both. This would lead to a safety issue. RUST will per definition avoid those situations by just invalidating the assigned Object (here b). If we would like to use both variables we have to make a deep copy. A deep copy is made via the function .clone().
Same applies for function calls.

This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *