Rust programming language

         Rust is the most beloved programming language according to developer survey by stackoverflow. I was curious why it was in the first place and apparently it had many features that make it a robust language for system programming. Rust is memory safe and hence resistant to memory leaks. Memory leak occurs when the programmer creates data on the heap but forgets to delete it after usage. And consequently may cause all the memory to get allocated and system may stop working. It is especially a problem for daemons and servers where there is an event loop to respond to requests indefinitely. A memory leak in each request would add up to eventually slow down the server.

Rust uses the concept of ownership, borrowing and lifetime to implement its memory safety without using a garbage collector as in more abstract languages like python and js which do not have manual memory management. In python, java, we do not have control where the variables are stored. Languages like c++ have more control over where the variables are stored and hence can potentially cause invalid memory access or cyclic references. There are two types of memory in a program, stack and heap. Stack data must be of fixed size while heap data can be of any size. Programs don't have direct access to the heap data as they are of an arbitrary size. Hence, they are kept track using pointers. Stack memory is dropped when it is out of scope while Garbage collector automatically deletes the heap data when it is no longer needed.

In c++, there is no garbage collector and we have to deallocate the allocated memory manually and carefully handle access to freed data to prevent memory leaks. However, while Garbage collector is better for memory issues, it also causes lower performance. Rust's unique concepts of ownership and borrowing help in getting memory safe applications without the overhead of garbage collector. By following a set of rules, the rust's borrow checker, the part of rust that enforces memory management with ownership, can understand where to initialise data and where to free the data. Giving us the performance level of c++ and memory safety of garbage collector.


Even rust can potentially create memory leaks. For reference check [https://fly.io/blog/rust-memory-leak/  

https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/second-edition/ch15-06-reference-cycles.html#:~:text=We%20can%20see%20that%20Rust,values%20will%20never%20be%20dropped.

https://onesignal.com/blog/solving-memory-leaks-in-rust/

Rust's memory safe is specifically to prevent invalid memory access. We can still leak memory like using Rc and RefCell where the references point to each other. 

Rust just protects from invalid memory access, not to prevent resource starvation and a starved program is potentially killed by the os.

Also, rust has a steep learning curve for beginners and takes 

Even so, rust minimizes the chances of memory leaks. Hence, its suitability in creating os kernel, microcontroller applications and other low level os and network stack applications.  Linux kernels now have implementations in rust and many large companies have begun to switch from C++ to rust for newer project. While rust can also be used for web development, game development, etc. most of the current frameworks aren't mature nor are they production ready and easy to use resulting in higher development time. The use cases have to be properly analyzed and compared with alternatives for cost and performance before implementation.

 Knowledge Assam provides digital service for the digital growth of your business. Our services are as follows :

  • Website Development
  • App Development
  • Digital Marketing
  • Advertising
  • Video Editing


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.