Rust Through The Eyes Of A Go Developer

Hi everyone, I’m Tiger.
This time, I want to talk about Rust.What’s so great about Rust?As an old user of Go, how do I view Rust?Let’s have a good discussion on this topic today.
One of the most troublesome issues for us programmers is memory management, such as dangling pointers, memory leaks, data races, etc., which can really make you want to “pull your hair out” 🤦♂️.

Rust Through The Eyes Of A Go Developer

One of the biggest highlights of Rust is its astonishing memory safety mechanism.
Rust’s memory management is achieved through mechanisms like ownership, borrowing, and lifetimes. Unlike Go’s garbage collection, Rust eliminates memory issues at compile time. For example, consider the following small example:
let s1 = String::from("Hello, Rust!");let s2 = s1; // println!("{}", s1); // This line will cause a compile error because the ownership of s1 has been transferred to s2println!("{}", s2);
In this example, the ownership of s1 has been transferred to s2, and the compiler sternly tells you: “Brother, you can’t use s1 anymore!” This rule may seem harsh, but it actually helps us avoid many potential memory management issues, such as dangling pointers and memory leaks.
When writing code in Rust, you will feel that the compiler handles the hidden dangers you might not even think of very well.
Speaking of performance, this is another major selling point of Rust. As we all know, there is usually a contradiction in programming languages: either you pursue high performance and have to write a bunch of low-level code; or you choose high-level abstractions, but performance may be compromised. However, Rust breaks this curse, claiming to offer “zero-cost abstractions.”
What is zero-cost abstraction? Simply put, it means that the high-level abstract code you write in Rust can be optimized by the compiler to be as efficient as low-level code. Isn’t this the perfect state of having both fish and bear’s paw? Take a look at this generic function example:
fn find_largest<T: PartialOrd>(list: &[T]) -> &T {    let mut largest = &list[0];    for item in list.iter() {        if item > largest {            largest = item;        }    }    largest}fn main() {    let numbers = vec![34, 50, 25, 100, 65];    let largest_number = find_largest(&numbers);    println!("The largest number is {}", largest_number);}
In this example, the find_largest function accepts an array of any type that implements the PartialOrd trait and returns the largest element in the array. The Rust compiler generates the corresponding machine code based on the specific type, ensuring the program’s execution efficiency. Not only is the code clear and understandable, but there is also no compromise on performance.

Concurrent Programming: Multithreading Safety Is Fundamental

As a Go developer, when it comes to concurrent programming, what we first think of is goroutines and channels, which are also one of Go’s killer features. However, Rust is not to be outdone in concurrent programming. Rust’s concurrency model is based on threads and message-passing mechanisms, ensuring thread safety through its ownership system.
Let’s take a look at this simple Rust concurrent code:
use std::sync::mpsc;use std::thread;use std::time::Duration;fn main() {    let (tx, rx) = mpsc::channel();    thread::spawn(move || {        let message = String::from("Hello from another thread!");        tx.send(message).unwrap();        thread::sleep(Duration::from_millis(500));    });    let received = rx.recv().unwrap();    println!("Received: {}", received);}
In this example, the main thread creates a channel and passes the sending end tx to the new thread. The new thread sends a message via tx, and the main thread receives and prints the message.
The channel mechanism in Rust is somewhat similar to Go’s channels, but it ensures that there will be no issues due to data contention between threads through the concept of ownership.

Rust vs Go

After discussing so many advantages of Rust, as a long-time Go user, I must be honest: while Rust is powerful, it is not omnipotent.
First of all, the learning curve of Rust is indeed much steeper than that of Go. Go advocates that “simplicity is beauty,” allowing developers to quickly get started and dive into production, while Rust is more complex in both syntax and concepts. This means that for teams pursuing development efficiency, Go may still be the better choice.
Additionally, Rust’s ecosystem is relatively less mature than Go’s. Although the Rust community is developing rapidly and the package management tool Cargo is very powerful, in some areas, Rust’s libraries and tools are still not mature enough.
On the other hand, Go has a very rich and mature ecosystem in fields like web development, microservices, and cloud computing, making it more natural for developers to choose Go in these areas.
Therefore, both Rust and Go have their own advantages, and the choice of language depends on specific needs. For example, if you are doing system-level programming, pursuing extreme performance and safety, Rust is definitely the best choice. But if you are developing distributed systems or cloud services, Go might be more suitable for you.
Finally, I want to say that whether it’s Go or Rust, both are indispensable parts of our toolbox. 🤓
Currently, for those interested in programming and the workplace, you can contact me on WeChat: golang404, and I will add you to the “Programmer Group Chat.”

🔥 Tiger’s Private Collection of Quality Recommendations 🔥

Tiger, as an old coder, has compiled the most comprehensive“GO Backend Development Resource Collection”.

The resources include “IDEA Video Tutorials”, “The Most Comprehensive GO Interview Question Bank”, “The Most Comprehensive Project Practical Source Code and Videos”, and “Graduation Design System Source Code”, totaling up to 650GB. All of it is free to obtain! Fully meeting the learning needs of programmers at various stages!

Leave a Comment