“If you want your application in production for 10+ years with very less need of maintenance prefer Rust else Go”
As per StackOverflow survey 2020 , Rust is the most beloved language in the developer community .
So what makes Rust so amazing even than Python or TypeScript ? Why are people suggesting to write the whole Linux kernel in Rust ? We will answer all these questions and explore the uniqueness of Rust. We will discuss about:
- Features of Rust that are different from other languages
- Companies who use Rust and why they use it
- Cons of Rust
- Conclusion whether you should learn Rust or not in 2022
So let’s begin
Introduction
Rust is a statically typed programming language without a garbage collector (yes , it doesn’t have a garbage collector) and with more focus on safety, speed and more low level control without sacrificing the high level syntax. It's developed by keeping in mind the core issues of the today's high level languages, for example data races conditions in multithreading programs.
Rust was originally made as a side project at Mozilla Research by Graydon Hoare. It was first launched on 7th July 2010.
Today many tech giants like AWS , Microsoft use Rust in production . It’s new way of managing memory has made developers write bug free code (not completely free) without sacrificing speed and performance . It can be used in different domains like Web, Blockchain , Game , Networking and even for Embedded devices because it’s minimal (or no) runtime. Let’s see what Rust has to offer
Unique features of Rust that no other language provide
The ownership model
If you are a C/C++ developer then you have experienced the problem of dangling pointers aka when a pointer points to an already freed memory location . In such a situation your programs crash. This is a major issue which is present in many languages , even if they don’t have pointers . The used garbage collector to automatically deallocate memory instead of the programmer has to do. But this is also not a full proof solution . Garbage collector instead has its own bugs. Due to such bugs and inappropriate memory management lead to memory leaks and memory related security issues.
Rust tries to solve this issue using it’s ownership model concept. In rust there is a concept of ownership. If we pass an object to a function , instead of passing it as by reference the parameters of the function takes the ownership of the object. Let’s take an example to get a clear picture:
#[derive(Debug)]
struct Person{
name: String,
age: u64,
}
fn show_struct(person_obj: Person){
println!("Name is: {} and age is {}",person_obj.name,person_obj.age);
}
fn main() {
let person1 = Person{
name: String::from("Rishabh Raghwendra"),
age:19
};
show_struct(person1);
println!("{:#?}",person1); // here we get the error
}
If you run this code in , Rust compiler will give you an error borrow of moved value:
person1
.
value borrowed here after move
We have created a struct called Person and an object of it in main() as person1. We then passed the object to function show_struct() to print the object contents. After that we tried to print the person1. The reason we get the error at the print statement because of person1 is no longer the owner of the object.
When we passed the person1 to the show_struct() function , it’s parameters took the ownership of the person1 object. Hence person1 is no longer the owner of the object.
There is a way to not let the function parameters take the ownership , by passing it as pass by reference as we do in C/C++ . It is out of the scope of the blog so we will discuss about in my future blog.
This model prevents the misuse of memory without the need of a garbage collector.
Fearless Concurrency
Ever run in a data race condition or deadlock situation while doing multi-threading programming ? If yes , then you definitely run in any one of these situations and you even know how hard it is to write a correct multi-thread program and also how hard to debug it if something bad happens in production . Ahhhhh!!! Never push your code on Fridays.
Rust has special attention to concurrency and memory safety . It gives you errors at compile time rather than at runtime. Rust again uses an ownership model to prevent access to invalid memory . For eg take a look at this Rust code:
use std::thread;
fn main() {
let v = vec![1, 2, 3];
let handle = thread::spawn(|| {
println!("Here's a vector: {:?}", v);
});
drop(v); // oh no!
handle.join().unwrap();
}
We spawn a thread with a closure who is accessing vector ‘v’ and store the thread instance in variable ‘handle’. After that we tried to drop the (free up) vector ‘v’.
If you implement the same logic in other programming languages like C/C++ or Python . You will get a Segmentation Fault error which will lead your program to crash and hackers to gain access to your system , nice.
If you run this code in Rust . It will give you an error saying that you don’t have the right to drop the variable ‘v’ because its value is moved to the thread . The ownership model kicked in!!. It does so because rust compilers have trust issues with us (thread: what’s the guarantee that ‘v’ will be valid till I am doing my work). That's why Rust compiler asks you to give the ownership of the variable to the thread so that it can fearlessly use the variable ‘v’. To see the solution of the above error see this link.
See how rust stopped you from making a nasty concurrency bug at compile time and saved your weekend . Such type of development Rustaceans called Compiler Driven Development.
Friendly Compiler
If you have run the above examples (you can run it in Rust Playground) , you have seen that the compiler gives you solutions/hints to the errors too. Rust compiler is very intelligent and tries to give you the appropriate solution to the error in a friendly readable way. This is one of the features that made developers love Rust.
Companies who uses Rust in Production
Microsoft
Microsoft has now started to migrate their C/C++ codebase to Rust . The reason is that they have found that most security issues are related to memory. They tried to solve the issue on their own using C/C++ but failed to do so . The only option they are left with is to migrate their C/C++ codebase to Rust which prevents 90% of the memory related issues at compile time only.
For more info watch this video.
Facebook used Rust to rewrite their source control backend which was originally written in Python . Rust attracted their attention because they want speed & memory safety which is only Rust's able guarantee .
For more info watch this video.
Discord
Discord has written both their client-side and server-side in Rust. They rewritten the Read state service in Rust which was originally written in Go. Due to the Go garbage collector they see unexpected high latency spikes randomly. The only solution they are left with is to rewrite everything in Rust because it doesn’t have a garbage collector and ensures memory safety. Till now their experience with Rust is very positive and the codebase is easily able to serve 11 million users at a time.
For more info read this official discord blog.
NPM
Engineers at NPM realized that their service performance would soon become a bottleneck if the user increased. They refused to use C/C++ and Java because they don’t provide memory safety and they don’t want to deploy JVM on their servers. The only best fit as per their requirements is Rust. They believe it’s the right choice they have made, as Rust has the most friendly compiler and community. They also have positive experience with Rust till now.
For more info read this official Rust whitepaper
Cons of Rust
Rust has steep learning Curve
Rust involves lot’s of new programming concepts which you haven’t heard of or used in any of the programming languages like Python , Java , C++ . For eg as we have seen there are concepts of ownership , it also has a concept known as lifetimes and these are the major concepts of Rust and most of the time Rust developers find themselves fighting with the compiler. Hence , Rust is not preferred to be your first language.
Slow compilations time
It has slow compilation time. For bigger projects it can be more than 5 mins . Even it uses some methods to reduce the compilation time but it doesn’t have much effect. Rust compilation time has decreased from the legacy releases but still it’s slow and most importantly if your machine doesn’t have enough RAM then it may slow down your machine performance. Therefore , for now , it’s recommended to use some host servers to compile the Rust code , as it can drastically reduce the compilation time.
Small developer community
Rust is a new language , hence it’s community is not so big . You can get stuck in some errors but there is no solution for it on google or stackoverflow even. But this is not a big issue . You can join their discord server , there are already more than 2k+ developers on the server , where you can get 24x7 support . It’s a very welcoming community and you should be a part of it if you are learning Rust.
Conclusion: Whether you should learn Rust or not in 2022
In my opinion , Rust has a bright future . Despite not being a beginner friendly or smaller developer community , it has lots of features and memory safety guarantees which other languages don’t . If you are a beginner , just starting out your career in programming then Rust is not a best fit for you . It may demotivate you from programming . So it’s best to learn other languages like Python , Java and when you become comfortable with them then learn Rust. Otherwise if you want to learn system programming then Rust can be a best choice for you.
If you have any doubts , feel free to comment down below . I will be happy to help you.
To see my projects or to get in personal touch , follow me on:
Linkedin:https://www.linkedin.com/in/rishabh-sde/
GitHub: https://github.com/Rishabhraghwendra18
Thanks for reading
Written with ❤️ & passion 🔥 by Rishabh Raghwendra
Top comments (0)