If you stumbled upon this article, either you have already heard and is interested in Rust programming language, or you may already be working with it. But here is a brief intro

Design Dig - Rust Design Principles

Rust is a multi-paradigm programming language focused on performance and safety, especially safe concurrency. Rust is syntactically similar to C++ but provides memory safety without using garbage collection. — Wiki

So Rust has a lot of hype around memory safety and garbage collector. In this design dig, we would look into Rust design principals

Code Monitor Photo by Markus Spiske on Unsplash


Memory safety

So what makes Rust memory safe is that your code can not be unsafe accidentally. Yes, you heard that write, Rust still provides a way to do unsafe code if one wishes to, but it is not advised. There are few design principals that Rust strictly to keep your code memory safe. We are going to talk about those principals

Ownership

In Rust, a variable declared within a scope never outlive that scope, meaning that memory allocated to a variable is de-allocated as soon as that scope returns to the caller scope, hence freeing and managing memory efficiently. In this context scope that defines variable is the owner of that variable

But Rust also allows you to pass ownership, like returning a variable from scope to caller scope when execution ends passes ownership of that variable to caller scope. Passing a variable to a child scope (in a certain way) also transfers scope

This way, Rust makes sure that at any given time variable is referenced by only one owner not multiple. Ownership lies with a scope that owns it either by transfer or by initialization.

Borrowing/Lending

Since transferring ownership means; the scope that transferred, no longer have control over the transferred variable, hence it can not refer to, or use it. This is sometimes not feasible in software development.

To overcome this Rust provides a way for a scope to lend a variable to other scopes (variable is borrowed by scope it is being transferred to). A variable passed via reference &<variableName> is how values are lent temporarily.

A variable can be lent to multiple scopes simultaneously, then how could it be memory safe. The way Rust approaches it is that variable lent is read-only by default. When a variable lent with mutability allowed, Rust ensures that it is only lent to one scope at any given time. Rust enforces following restrictions when a variable is shared with mutability allowed

  • There can only be one active reference at any given time

  • No other reference; even read-only is not allowed, simultaneously

This avoids race conditions, ensuring memory safety. For further reading with examples, read here

Books over Kofee Photo by Debby Hudson on Unsplash


Memory management

Rust has very little or no runtime and hence do not need a garbage collector. Rust neither has a garbage collector nor does it has automatic reference counting. Instead, Rust uses, RAII (Resource Acquisition Is Initialization), where memory allocation is done at the construction phase and de-allocated at destruction phase

Though Rust allows explicit reference counting by using rc and atomic reference counting by using arc , while categorically making developer aware that RC is not thread-safe


Statically and Strongly typed

This may not come as a surprise but it is, statically typed meaning that all types are known at compile-time, like C and C++, whereas, strongly typed means that compiler makes it harder to write an incorrect program. This strict compilation policy makes Rust memory safe, with complete control over memory as well as with no run time. That’s why Rust can be used to write embedded systems with very low memory footprint as well as highly abstracted cloud applications.


About The Author

I am Pankaj Baagwan, a System Design Architect. A Computer Scientist by heart, process enthusiast, and open source author/contributor/writer. Advocates Karma. Love working with cutting edge, fascinating, open source technologies.

  • To consult Pankaj Bagwan on System Design, Cyber Security and Application Development, SEO and SMO, please reach out at me[at]bagwanpankaj[dot]com

  • For promotion/advertisement of your services and products on this blog, please reach out at me[at]bagwanpankaj[dot]com

Stay tuned <3. Signing off for RAAM