Re: [討論] Rust與C++

作者: shyin7089 (shyin7089)   2023-06-27 09:02:09
※ 引述《icetofux ()》之銘言:
: 最近看到一則windows以Rust改寫部分程式的新聞,仔細一查發現Linux似乎也打算將Rust作為內核開發的第二語言,讓我對Rust這個語言產生很大的興趣。
: 我最先學會的語言是C,後來在學習物件導向的時候開始接觸到C++,對於C++提供的語言特性及STL印象深刻。雖然不能說寫的非常好,但感覺C++強化了很多C在記憶體管理上容易出現問題的地方。目前如果環境許可,C++往往是我會優先選擇的語言之一。
: 快速搜尋了一下Rust及C++的比較,大部分都是著重在將兩個語言的優缺點條列比較,比如Rust在記憶體管理跟編譯訊息上更為突出,而C++則是保持對C的相容跟編譯速度更為出色,編譯出來的執行效率兩者似乎在伯仲之間,都非常的優秀。
: 但是看了幾項比較資料後,卻沒有發現兩大作業系統選擇了較為年輕的Rust而非長時間發展的C++的關鍵原因。請問有對Rust較為熟悉的先進可以幫忙解惑嗎?
: 謝謝。
首先,先澄清一下以避免誤解。
Linux 核心的內部主要子系統並沒有轉以 Rust 撰寫,而是以 module 的形式,依附於現
有的核心架構,以 Rust 語言開發新的 driver 。
OK ,回到正題。
剛好在前陣子也看到 Linus 對 Rust 跟 C++ 的一些想法,挺有趣在此分享一下。
簡單來說, Linux 不採用 C++ 的原因是因為 "C++ solves _none_ of the C issues,
and only makes things worse. It really is a crap language."。
而 Rust 會被採納是因為它很明顯的針對了 C 語言的記憶體缺陷有很大的改善,反而是
C++ 提出改進,老實說在 C 語言 + GCC (GNU extension) 的組合拳下也並不是實作不出來。
在 1992 年跟 2004 年時,不管是核心或是 module ,都有人想用 C++ 去撰寫、改寫 [1]。
在此引述當時 Linus 的留言:
The fact is, C++ compilers are not trustworthy. They were even worse in
1992, but some fundamental facts haven't changed:
- the whole C++ exception handling thing is fundamentally broken. It's
_especially_ broken for kernels.
- any compiler or language that likes to hide things like memory
allocations behind your back just isn't a good choice for a kernel.
- you can write object-oriented code (useful for filesystems etc) in C,
_without_ the crap that is C++.
In general, I'd say that anybody who designs his kernel modules for C++ is
either
(a) looking for problems
(b) a C++ bigot that can't see what he is writing is really just C anyway
(c) was given an assignment in CS class to do so.
Feel free to make up (d).
但這不是說所有 C++ 已有的 feature 都是垃圾,只是 Linus 對於作業系統的撰寫有一
定嚴格要求程式碼要能更清楚的闡明它的執行步驟,並且對於物件以及記憶體的操作要夠
明確。
這點可以從近期要引入 pointer and lock guards [2] 的討論中看出端倪 [8]。
除此之外,其實撰寫核心的 C 語言也有其限制,跟一般在 userspace 的 C 不太一樣。
比如不能使用 floating point, setjmp/longjmp 等,即便他們是 standard 。
因此,對於 C++ 而言, Linus 只是討厭有問題的程式碼以及會讓程式碼出問題的 C++
、它的函式庫 [7] 以及它的(不成熟的)編譯器而已。而開發者們則是對於 C++ 的引入,在
思考時所想的壞處多到以至於不想去驗證它的好處有哪些而已 [5][6]。引述至 [7] :
C++ leads to really really bad design choices. You invariably start using
the "nice" library features of the language like STL and Boost and other
total and utter crap, that may "help" you program, but causes:
- infinite amounts of pain when they don't work (and anybody who tells me
that STL and especially Boost are stable and portable is just so full
of BS that it's not even funny)
- inefficient abstracted programming models where two years down the road
you notice that some abstraction wasn't very efficient, but now all
your code depends on all the nice object models around it, and you
cannot fix it without rewriting your app.
In other words, the only way to do good, efficient, and system-level and
portable C++ ends up to limit yourself to all the things that are
basically available in C. And limiting your project to C means that people
don't screw that up, and also means that you get a lot of programmers that
do actually understand low-level issues and don't screw things up with any
idiotic "object model" crap.
而在近一次的訪談中 [4], Linus 也有被問到為何不使用 C++ ,以下是訪談片段:
By using Rust in the Linux kernel, our hope is that:
- New code written in Rust has a reduced risk of memory safety bugs, data
races and logic bugs overall, thanks to the language properties mentioned
below;
- Maintainers are more confident in refactoring and accepting patches for
modules, thanks to the safe subset of Rust;
- New drivers and modules become easier to write, thanks to abstractions that
are easier to reason about, based on modern language features, as well as
backed by detailed documentation;
More people get involved overall in developing the kernel, thanks to the
usage of a modern language; and
- By taking advantage of Rust tooling, we keep enforcing the documentation
guidelines we have established so far in the project. For instance, we
require having all public APIs, safety preconditions, `unsafe` blocks and
type invariants documented.
可以看到, Rust 的引入不僅可以解決記憶體安全問題;讓 driver maintainer 更容易接
受新的 patch ; 強制撰寫 documentation 等,都是一些就算引入 C++ 後也不會有的改
進。
尤其是 doc 的部分, Rust 真的做得蠻好的。
總而言之,因為語言特性、編譯器的不成熟, C++ 不適合 OS kernel 的開發,而
memory safety 的 Rust 適合。
[1] https://lore.kernel.org/all/[email protected]/T/#u
[2] https://lore.kernel.org/all/[email protected]/T/#u
[3] https://lore.kernel.org/rcu/CAHk-=wgaSkM4fjdP9dcdXQpLLjxW43ykgLA=FgzyHpyHayz8ww@mail.gmail.com/
[4] https://itwire.com/business-it-news/open-source/rust-support-in-linux-may-be-possible-by-5-14-release-torvalds.html
[5] https://lore.kernel.org/all/[email protected]/
[6] https://lore.kernel.org/all/[email protected]/
[7] https://harmful.cat-v.org/software/c++/linus
[8] https://lore.kernel.org/lkml/CAHk-=wgaSkM4fjdP9dcdXQpLLjxW43ykgLA=FgzyHpyHayz8ww@mail.gmail.com/
作者: pponywong (pony)   2023-06-27 11:35:00
推 c++ 這幾年的發展 的確跟kernel 面對的問題不一樣
作者: Schottky (順風相送)   2023-06-27 15:31:00
還真沒注意過 kernel 不能用 floating point 這回事
作者: james732 (好人超)   2023-06-27 16:34:00
Linus回答這個問題應該答到很膩了XD
作者: DaOppaiLoli (大歐派蘿莉)   2023-06-28 00:07:00
推推長知識了
作者: rnoro (隼)   2023-06-28 00:18:00
真是好文!學到很多XD
作者: james732 (好人超)   2023-06-28 00:29:00
至少他沒有說Fuck C++ XD
作者: tinlans ( )   2023-06-28 01:45:00
可能近年都在寫交易系統的關係,比起 Rust 我更關注 Carbon如果還留在半導體業我大概也會直接押寶 Rust
作者: icetofux   2023-06-28 08:20:00
謝謝你的說明,完整的解答了我的疑惑。
作者: laxw (鳥好)   2023-06-28 17:30:00
推好文
作者: kyushu (蘇打綠嚇倒我了)   2023-06-28 18:01:00
請問rust跟c++互call方便嗎?因爲現有c++ open source 還是比較多,想改謝rust call c++ library改寫成rust 乎叫 c++ library
作者: flarehunter (Range)   2023-06-28 20:56:00
https://cxx.rs/ 辦得到 但看起來好像有一點麻煩
作者: v86861062 (數字人:3)   2023-06-29 12:57:00
推推
作者: dzwei (Cout<< *p << \n ;)   2023-06-29 15:06:00
互摳是可以治標,但最根本的還是那句話:Rewriting everything with rust
作者: wulouise (在線上!=在電腦前)   2023-06-30 21:46:00
rust的好處就是你從頭開始寫就少很多問題,carbon則是在你已經有c++的情況下要怎麼讓問題減少rust支援c interop吧,但是支援之後那邊就成了會出問題的地方
作者: Schottky (順風相送)   2023-07-01 22:09:00
同意 Linus 的脾氣變好很多 XD
作者: Richun (解放左手的OO之力)   2023-07-01 22:39:00
我記得剛開始rust想進入kernel時,Linus有先批評rust的錯誤處理,主要是有些lib在出錯誤的情況下就直接panic掉,在kernel裡面這處理方法不能被接受,後來怎改的我就不知道。
作者: saladim (殺拉頂)   2023-07-08 03:58:00
這句話: "...反而是C++ 提出改進,老實說在 C 語言 +..."是說C++提出改進而不是Rust??
作者: shyin7089 (shyin7089)   2023-07-11 19:13:00
對。例如 OO 的概念, C 語言也可以實作出來。
作者: dzwei (Cout<< *p << \n ;)   2023-07-12 00:19:00
rust的trait概念也跟x/c++的oo不太一樣就是XD
作者: dmeiki (熊麻吉)   2023-08-21 16:35:00

Links booklink

Contact Us: admin [ a t ] ucptt.com