<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Rust on QSysArch - Quantum Computer System Architecture</title><link>https://qsysarch.com/zh-hk/categories/rust/</link><description>Recent content in Rust on QSysArch - Quantum Computer System Architecture</description><generator>Hugo</generator><language>zh-hk</language><lastBuildDate>Sun, 14 Sep 2025 00:00:00 +0000</lastBuildDate><atom:link href="https://qsysarch.com/zh-hk/categories/rust/index.xml" rel="self" type="application/rss+xml"/><item><title>Rust的秘訣在於：更精確的類型？</title><link>https://qsysarch.com/zh-hk/posts/0004-rust-to-the-rescue/</link><pubDate>Sun, 14 Sep 2025 00:00:00 +0000</pubDate><guid>https://qsysarch.com/zh-hk/posts/0004-rust-to-the-rescue/</guid><description>&lt;img src='https://qsysarch.com/images/cpu-gpu-simd.webp' style="width:280px;max-width:100%;float:right;padding-left:20px;"&gt;&#10;&lt;p&gt;Ralf Levien 以其高效實現的全球最快字形渲染引擎而聞名，該引擎採用了非常巧妙的基於 Rust 的 SIMD 設計。 2023 年我在 Bestechnics 設計 SIMD GPU 時，他的工作給了我很大的啟發。後來，Ralf Levien 也開發了該字形渲染引擎的 GPU 版本。&lt;/p&gt;&#10;&lt;p&gt;所以，當我偶然看到他關於「Rust 如何獲勝：追求高效能、高可靠性軟體」的最新演講時，我決定花一個小時觀看演講，並從中汲取任何值得學習的見解。&lt;/p&gt;&#10;&lt;p&gt;&#10;&lt;img class="glightbox" src="https://qsysarch.com/images/rust-secret-sauce.webp" alt="Rust Secret Sauce: More precise types" /&gt;&lt;/p&gt;&#10;&lt;h1 id="問題陳述"&gt;&#10; 問題陳述&#10; &lt;a class="heading-link" href="#%e5%95%8f%e9%a1%8c%e9%99%b3%e8%bf%b0"&gt;&#10; &lt;i class="fa-solid fa-link" aria-hidden="true" title="Link to heading"&gt;&lt;/i&gt;&#10; &lt;span class="sr-only"&gt;Link to heading&lt;/span&gt;&#10; &lt;/a&gt;&#10;&lt;/h1&gt;&#10;&lt;p&gt;緊張的部分（也稱為溺水區）大約從 55:00 開始，幻燈片標題為「&lt;em&gt;Rust 秘方：更精確的字體&lt;/em&gt;」。&lt;/p&gt;&#10;&lt;img src='https://qsysarch.com/images/progressive-insight-and-the-drowing-zone.webp' style="width:500px;max-width:100%;float:right;padding-left:20px;"&gt;&#10;&lt;p&gt;在 56 分 50 秒，Raph 說：“這就是仿射類型發揮作用的地方；你想把（可克隆性）建模為‘存在一個實例’；而這種類型允許你在函數簽名中仔細指定如何處理所有權；最後一個例子我就略過了，不過挺有意思的，對吧……”&lt;/p&gt;&#10;&lt;p&gt;我絕對不敢自稱是 Rust 專家，而且我承認自己更偏愛 Go——所以，問題是，我能理解 &lt;a href="https://users.rust-lang.org/t/what-are-affine-types-in-rust/23755" class="external-link" target="_blank" rel="noopener"&gt;&lt;em&gt;affine type&lt;/em&gt;&lt;/a&gt; 的真正意義嗎？能否用簡單易懂的語言解釋一下，而不用讓我淹沒在浩瀚的知識海洋中？&lt;/p&gt;&#10;&lt;p&gt;YouTube評論裡也提到了一個問題：最後一個函數的回傳類型應該是&lt;code&gt;Cow&amp;lt;'a, [T]&amp;gt;&lt;/code&gt;，而不是&lt;code&gt;Cow&amp;lt;&amp;amp;'a [T]&amp;gt;&lt;/code&gt;。說實話，這看起來比量子演算法還要複雜。但也許並非如此！&lt;/p&gt;&#10;&lt;p&gt;那麼，讓我們仔細看看這些「更精準的類型秘訣」的例子：&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;fn α( l: &amp;amp;[T] ) → Vec&amp;lt;T&amp;gt; &lt;/code&gt;: 始終分配內存，輸入不受影響&lt;/p&gt;&#10;&lt;/li&gt;&#10;&lt;li&gt;&#10;&lt;p&gt;&lt;code&gt;fn β( l: Vec&amp;lt;T&amp;gt; ) → Vec&amp;lt;T&amp;gt; &lt;/code&gt;: 消耗輸入並重複使用分配的記憶體。&lt;/p&gt;</description></item></channel></rss>