DEV Community

Cover image for Rust changed my (superficial) way of thinking
Matei Tudose
Matei Tudose

Posted on

Rust changed my (superficial) way of thinking

This might sound like another clickbait article shilling Rust, but it isn't. I've also been in the same spot as you, wondering why there is so much hype with Rust, because at the beginning, it seemed like just another low-level language like C++. But it's not.

Nowadays, many programmers use exclusively programming languages with a garbage collector (Java, Go, Python, etc.) and often overlook how memory is managed to make all of this possible. The GC is seen as some kind of "black box" that does some magic with the program's memory. But then we have low-level languages like C or C++, that don't have a GC and instead have you allocate/deallocate memory manually. While this is better for performance reasons, doing this in practice becomes risky, as there aren't many safeguards preventing you from shooting yourself in the foot.

Rust solves this. It imposes a set of strict rules that you must adhere to when coding in it. By understanding the imposed "ownership model", you are forced to actually think before passing around variables. At the beginning, it might seem that it also adds a lot of abstractions like all the other languages, but these are actually the safeguards protecting you in different scenarios.

Let's look at this example:

fn greet(name: String) {
    println!("Hello, {name}!");
}

fn main() {
    let name = String::from("Alice");
    greet(name);
    println!("{}", name); // This gives a compilation error!
}
Enter fullscreen mode Exit fullscreen mode

In languages like C++ or Python, it seems perfectly natural to keep using a variable after passing it to a function.

In Rust, trying to access name after it's been moved causes a compile-time error. This prevents dangling pointers, use-after-free, and double-free scenarios because the compiler guarantees that only one owner exists for each resource at any moment (this is one of the 3 rules of the Rust ownership model).

However, this is just a simple example. There are many other ways Rust's ownership system and associated safeguards work together to prevent subtle and dangerous bugs that are common in other programming languages. For instance, Rust's borrowing rules ensure that you can have either multiple readers or one writer to a piece of data at any point, but never both simultaneously. This eliminates data races at compile time.

Together, these rules and checks create a robust system that guarantees memory safety, thread safety, and overall program correctness without the need for runtime overhead or garbage collection. This multi-layered protection is why Rust is increasingly chosen for building critical system components, where reliability, security and speed are paramount.

So here's my advice to any dev that hasn't still tried Rust: do it. It might seem hard at the beginning, but you gain a lot of insight and you start to realize how "privileged" you are when using GC languages.

Top comments (0)