Why Rust?
Hi there. This article is going to be a why a Bachelor in Electronics and Communication Engineering, instead of mastering in his core programming concepts like C and C++, wishes to move on to an emerging language like RUST and Go although there are many language like Java, Python, and so on. This article is mostly based on my perception on learning a new language apart from all the differences of being a non-computer science student. I’ll also compare the advantages, disadvantages that I saw while going through the documentation provided by the community and in the end, I’ll be pointing out why I thought of studying RUST in first place.
Why RUST???
RUST being one of the emerging language and being one of the fast growing languages for the past 4 years has gained momentum among non-computer science student because of its scalability feature among Embedded Controllers. Many bloggers have indeed pointed out that the syntactical similarity between C and C++ with RUST, but its feature goes way beyond syntactical similarities and functionality from my point of perception, I’ll be sharing some of my views that I encountered while going through RUST programming language.
RUST vs. Other Languages
Built in Package Manager
RUST, being one of the emerging languages might be difficult at first…. BUT, there is a catch. RUST is more oriented or inclined towards Performance, Memory, Long Term Usage, Error Avoidance and so on…. When seeing how a library would have been imported, In traditional programming languages like C, C++ and So on, libraries would be represented as a Header Files, Packages, and so on. But when RUST is considered, Everything is basically a package but represented as a term called as “Crates”. All the Crates that is developed by a developed can be sent to a repository called as crates.io. From this website, we can able to collaborate with the rich eco-system of RUST and can able to get their library and work with it. This Crates is managed by “Cargo”, the built-in package manager of RUST. You can learn more about it in
https://doc.rust-lang.org/book/ch01-03-hello-cargo.html
Performance
Second Factor, Performance. When considering performance stand point, RUST is seen to Out-Perform many programming languages. But there are some drawbacks to it. Most of the programming language is built in such a way that the errors occurs in a run-time so that developers might able to debug the error and able to make changes to it using exceptions or a try-catch block. But RUST Doesn’t have an exception, but has some functionality like Result and panic! Methods. But the developers of RUST have framed the type system in such a way that Errors are found in compile time itself. So While running the program, there won’t be any errors like NullPointerException and so on. BUT there is a trade-off, Because of this compile time error checking, the time taken for a program to compile might be high. The another advantage of RUST is that the compiled output is going to be a executable file if developed from Windows Environment. Therefore, when developed a program and shipped across, On the host PC, the necessity for a RUST Compiler is neglected that is the compiled code can be run using Command Prompt. But on the other hand, the correctness of the code will increase exponentially. You can learn more about it in
https://doc.rust-lang.org/book/ch09-00-error-handling.html
Memory Management
Third Factor, Memory and Variable Declaration. The main advantage of RUST comes in Memory Management and Variable Declaration Methods. The developers of RUST designed it in such a way that it is focused towards Memory Safety and therefore the issues with pointing to a non-valid data(Null Pointer) is avoided. Moreover, the variable declaration is somewhat unique when considering other programming languages. In typical programming language, We’ll be having a data-type declaration like whether it is a String, Integer and so on. But when RUST is concerned, we’ll be having a yet another features concerning about whether a variable is Mutable or Immutable. In RUST, by default, a Variable is Immutable which can be changed to be Mutable by using "mut" Keyword. This is one of the important factor to me because, being an Embedded enthusiast, if a variable is said to be immutable, It should be immutable. But during the course of development, sometimes by human mistake, an immutable variable might change to mutable, Thereby risking the entire code structure. This is preventable if RUST is concerned. Some of the other Memory Management Features are
1. Absents of Garbage Collector since Complier Acts as a Garbage Collector.
2. Memory Overhead is minimum or negligible.
3. Lifetime management can be done because of presence of Borrow Checker.
You can learn more about RUST Memory Management in
https://en.wikipedia.org/wiki/Rust_(programming_language)#Features
https://www.reddit.com/r/rust/comments/b742vu/if_rust_has_no_garbage_collector_how_does_it
The borrow checker - Guide to Rustc Development (rust-lang.org)
Web Framework and Networking Libraries
Fourth Factor, Additional Library for Web Frameworks and Networking. The emerging field of RUST is with regards to Web Server Implementation and Lifecycle Management. It is still in development in progress and some of the libraries that are available is
1. Tokio – Asynchronous Runtime Framework for writing networking applications.
https://docs.rs/tokio/1.4.0/tokio/
2. Hyper – A fast, Safe, HTTP Implementation written for RUST
https://docs.rs/hyper/0.14.5/hyper/
3. Tower – A library of modular and reusable components for building robust and safer networking client nad server
https://docs.rs/tower/0.4.6/tower/
And many more……..
RUST for Embedded Engineers. Why Did I Choose to learn it?
RUST for embedded is yet another a developing and yet gaining momentum among Embedded enthusiast like Us. There are certain drawbacks, yet it is expected to grow steadily. Some of the features might seems to be a drawback in some aspects, but in a longer run, RUST is seen to out-perform those drawbacks. A best example that can be stated here is the long compilation time. Although it results in a slow development, It is as fast as C or C++ Equivalent on the target development boards. And since all the errors were rectified during the compile-time, the end outcome is going to be an error free program that is well suited for a production grade embedded projects.
Myself, as an Embedded and an Hardware enthusiast, is flabbergasted by the scale to which RUST is actually able to comprise many newly developed Hardware architecture, and added to the fact that, the RUST developer team is indeed published a very good documentation about how to go about with RUST and hardware. Another feature that pulled me towards RUST is that, recently, Microsoft Azure IoT Edge can also be reframed using RUST Language. This is made possible because of the cross compilation to the Cortex-M Architecture and many other architecture made possible using RUST Compiler. There are indeed many blogs relating to the functionality of RTOS with RUST, which need further studying. To me, personally, I like to learn and work with RUST (apart from working with all the advantages listed above) is a sense of experimenting with the hardware that I presumably know about and to scale the possibility with a newer yet interesting programming concept and to study more about integration with micro-services like Web-Server, Event Logger, and also use RUST Framework along with RTOS if in even it is possible. And therefore, My final goal is to create a logical code and a microcontroller application, which will have a small memory foot print and can do wonders. Apart from all the features, There is yet more to be explored, I leave it to you guys to explore more and wish you all success!
Additional References:
For Normal Programming:
1. RUST Documentation - https://doc.rust-lang.org/book/title-page.html
2. Crates Documentation - https://crates.io/
3. Wikipedia - https://en.wikipedia.org/wiki/Rust_(programming_language)
For Embedded Programming
1. Article - https://www.bluefruit.co.uk/quality/is-rust-ready-for-embedded-software/
2. An RTOS for embedded systems in Rust(PDF) - https://esc.fnwi.uva.nl/thesis/centraal/files/f155044980.pdf
3. RUST Embedded Book - https://docs.rust-embedded.org/discovery/index.html
4. RUST on Raspberry Pi - https://github.com/rust-embedded/rust-raspberrypi-OS-tutorials
5. Run RUST on Arduino(Read through) - https://dev.to/creativcoder/how-to-run-rust-on-arduino-uno-40c0