# Hello Rust, 與AI/Python完美的結合 - 陳昭明(I code so i am) {%hackmd @HWDC/BJOE4qInR %} >#### 》[議程介紹](https://hwdc.ithome.com.tw/2024/session-page/3218) >#### 》[填寫議程滿意度問卷|回饋建言給辛苦的講者](https://forms.gle/343sQqxJnm96uNDn6) # Why Rust 2024 v1.81.0 ## 佳評如潮 連續多年最被Stackoverflow票選為最佳程式語言 Technology - Admired and Desired ## 大廠支持 * Google, AWS, Microsoft * Linus Torvalds: Rust will go into Linux 6.1 ## Rust is quitely taking over Generative AI inside of HuggingFace Hugging Face 投入大量人力開發 Rust 生成式 AI 相關模組 * Candle * TEI * Tokenizer * Safetensors ## 生態系支援 - 星星數逐漸上升:[The Top Programming Languages 2024 Typescript and Rust are among the rising stars](https://spectrum.ieee.org/top-programming-languages-2024) - [套件數上升](https://crates.io/) # Rust 簡介和優勢 ## 兼具 C、Python 優點 - 效能接近 C,保留 Address、Pointer 操作 - 穩定、安全:避免 segmentation fault & memory leak - 與 Python 語法相近 - 套件齊全 - 跨平台 ## 克服 Python 缺點 - 套件版本依賴 - python 需使用 venv 進行環境隔離 - 可編譯成執行檔 - PyInstaller:很肥,包含直譯器和套件與程式碼,並非是二進位檔案 - 效能提升 - Python 處理大量資料效能差,但 Rust 可以有效提升 ## 效能提升 - 預設變數都是 immutable - 數值資料型別 8~128 bits - 嚴格的運算規定,減少型別判斷 e.g. i32+i16 => error - 變數生命週期:變數超出有效範圍就立刻回收 - 效能直逼 C/C++,完勝 Python:很多 Python 套件都改用 Rust ## 安裝 1. 用rustup-init.exe 2. wsl/linux/mac:指令 ## Concurrency - 平行處理 Parallel Processing - 非同步處理 Asynchronous ## 實際應用 [以Rust開發一個網站,不是網頁喔!](https://ithelp.ithome.com.tw/articles/10345310) [awesome](https://github.com/rust-unofficial/awesome-rust) ## Memory safty - Segmentation Fault - Rust 的編譯器會偵錯,不會把整台機器搞爆 - Memory Leak - C 在使用 malloc 操作記憶體,若未釋放記憶體則會導致記憶體備用爆 - Rust 會在離開變數作用域時自動進行記憶體回收 ## 跨平台 - 安裝容易 - Cargo 套件管理,是跟專案資料夾的,不會影響到全域 - 可編譯 exe, dll - 可打包成套件 - FII(Foreign Function Interface) - C, C#, JS, Python 整合 WEB Assembly # 與 AI/Python 的完美結合 ## Python/ Rust FII Pyo3 crate 支援互相呼叫 maturin 可產生 wheel 檔案 ## 機器學習開發流程 ![image](https://hackmd.io/_uploads/BJKs57g6A.png) ## ML & DL Crates * Machine Learning: Linfa * Deep learning * tch-rs * Candle * Burn LLM with Candle