Rust for Gophers

王林
Release: 2024-07-19 14:08:58
Original
550 people have browsed it

Rust for Gophers

Read the original article on packagemain.tech

Should you Rust, or should you Go? Which language is better, and does that question even make sense? Let’s talk about Go versus Rust in 2024, with our special guest, John Arundel. John is the author of For the Love of Go, Cloud Native DevOps with Kubernetes, and many other books. He also teaches both Go and Rust, so it’ll be interesting to hear his perspective. Here’s the interview!

John, we seem to be hearing more and more about Rust these days. Can you give us a quick introduction? What is Rust for, and why does it exist?

Sure, that’s easy. Rust is a language for controlling elevators.

Are you kidding?

Not at all. Graydon Hoare, the originator of Rust, got frustrated when the elevators in his building kept breaking down due to software problems. He thought “Surely we can do better than this!” And the rest is history.

We can’t prevent all bugs, but we can at least use a programming language that eliminates some major categories of bugs, such as buffer overflows, data races, and “use after free” issues. So from the very beginning, Rust’s focus has been on building reliable software, automating many of the safety checks that good programmers do anyway, and helping to catch mistakes before they reach production.

That sounds like a good idea, but I get the impression that Rust is a hard language to learn—especially compared to Go. Is that fair, and if so, why?

Go and Rust are both trying, in different ways, to solve the same problem: it’s hard to write software at scale in traditional languages like C and C++, because the programmers have to be very experienced and knowledgeable to avoid making mistakes. Even experts can slip up from time to time, and it takes a long time to train programmers to become experts.

Go tackles this problem by radically simplifying the language: compared to something like C++, it has a lot less syntax to learn. So programmers can spend their time learning to write programs well, rather than mastering a big, complex language. Go is lean, but effective.

To use an analogy, it’s easier to learn to drive a tractor than to fly a spaceship. The tractor may be a humble, pragmatic machine, but it does its job perfectly, and it’s actually a better choice than the spaceship for many tasks: ploughing a field, for example.

I like your analogy. I guess Rust is the spaceship?

Yes, Rust is big, complicated, and powerful, combining many of the best ideas from traditional imperative languages like C with functional programming concepts borrowed from languages like Haskell and Lisp.

There’s more to learn in Rust than there is in Go, but then it does more! If you want to fly to Mars, a spaceship is a better choice than a tractor. Of course, it takes a little longer to train an astronaut than a tractor driver.

Go has built-in garbage collection, which is great for simplicity. How does memory management work in Rust, and is it a big challenge to learn?

Yes, garbage collection means you don’t have to worry about allocating and freeing memory yourself, as you do in languages like C++. That makes programming easier and eliminates all sorts of memory-related bugs. On the other hand, you need a relatively complex runtime, and garbage collection affects performance.

Rust takes a different approach. It reclaims memory automatically, but without having to pause the program. It can do this by keeping track of all the references to a particular piece of data that exist. When no part of the program can refer to the data any more, Rust knows that bit of memory can be safely recycled straight away.

Yes, I’ve heard Rust has a strong focus on ownership and borrowing. How do these concepts compare to working with pointers in Go, and what are some good ways to wrap my head around them?

Well, I have good news—if you’re already used to pointers in Go, then references in Rust work basically the same way, only safer. If you create a mutable reference to a variable, it works just like a Go pointer: you can pass it to a function, or store it somewhere.

But, unlike in Go, as long as that mutable reference exists, it has exclusive access to the data: no one else can modify it, or even read it. In Go terms, it’s like having an automatic mutex lock. And when a function doesn’t need to modify the data, it can instead borrow a shared reference, which is read-only, and lots of them can exist at once.

Rust 還追蹤原始資料:當它超出範圍時,對它的任何引用都不再有效。因此,編譯器可以偵測到多種懸空指標錯誤,在這些錯誤中,您嘗試使用對不再存在的值的參考。這會導致未定義的行為,這是一種很好的方式來表達可怕的事情將會發生,而 Rust 的價值主張的一部分是「永遠沒有未定義的行為」。

在 Rust 中,我們必須找到一種編寫程式的方法,以便對資料的引用始終有效,並且一次只存在一個可變引用。這需要一些時間來適應(Rust 程式設計師稱之為「對抗借用檢查器」),但產生的程式更可靠,而且更有可能是正確的。

例如,所有Go 程式設計師都熟悉數據競爭,即兩個或多個goroutine 嘗試同時存取某些共享數據,結果不可預測:最好的情況是程式會崩潰,最壞的情況是程式會繼續損壞或無效資料。

在 Rust 中,這樣的程式無法編譯!所有權和引用規則意味著對同一事物的兩個可變引用不能同時存在。你只需要以不同的方式解決問題。

這讓我們很清楚地了解了並發性。我喜歡 Go 的通道和協程的並發特性。 Rust 如何處理並發性,我的 Go 經驗是否有任何相似之處?

是的,goroutine 和通道很棒:超輕量級的任務抽象,與傳統的多執行緒相比非常便宜。另一方面,Go 只為我們提供了基本的構建塊:我們有責任確保安全地使用它們,避免資料爭用或死鎖。這可能很難做到!

Rust 沒有 goroutine,但它有非同步任務,這很像 goroutine,只是具有通常的 Rust 安全保證。還有一些優秀的第三方框架,例如 Tokio 和 Rayon,它們可以取得一堆資料並自動找出最有效的平行處理方法。

因此,雖然並發程序總是很難寫好,但如果你能在 Go 中做到這一點,你會發現這些技能也可以很好地轉移到 Rust。

我喜歡邊做邊學。對於開始使用 Rust 的 Go 程式設計師,您有什麼好的實作練習或專案推薦嗎,例如 Go 之旅?

Rusdlings 是一個很好的起點:它是一組互動式小練習,可引導您完成所有語言基礎。如果您想從真實的人類那裡獲得反饋,請查看 Exercism 上的 Rust 曲目。還有 Rust by Example,這是用於工作範例片段的絕佳資源。

聽起來您是兩種語言的粉絲。您比較喜歡哪一個?您是否會建議已經了解 Go 的人也嘗試學習 Rust?

是的,Go 和 Rust 分別吸引了我大腦的不同部位。我喜歡 Go 的極端簡單性和實用主義:它用很少的資源做很多事情,並且很好地解決了大多數問題。

另一方面,Rust 巧妙地填補了 Go 不是理想選擇的空白:核心、韌體、嵌入式設備以及醫療設備、工業、航空航天等安全關鍵應用程式。

當然還有電梯。

當然!所以我喜歡 Rust 的這一方面,它也是一種非常有趣且富有表現力的語言。我認為任何人都值得花時間來玩 Rust,並堅持足夠長的時間來克服最初對語法的不熟悉,以及與借用檢查器的鬥爭。

即使您認為 Rust 不適合您,您也會學到一些有趣的思考問題的新方法,並且它還可以幫助您更多地了解 Go 所做的不同權衡。

從職業角度來看,Go 和 Rust 在可預見的未來都將是非常有價值的技能。我認為很快,我們將討論“Go 和 Rust 與其他一切”,而不是“Go 與 Rust”。

John,感謝您成為我們的客人,並向我們提供您對 Go 和 Rust 的看法。人們可以在哪裡找到有關您的更多信息,例如,他們是否對您的書籍或培訓課程感興趣?

這是我的榮幸!如果您想了解更多信息,請訪問我的網站 bitfieldconsulting.com,或聯繫我 — 我很樂意與您聊天。

The above is the detailed content of Rust for Gophers. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!