OSCHINA
5 月 15 日是 Rust 1.0 發布十周年紀念日,Rust 項目開發者在荷蘭的 Utrecht 舉辦了“Rust 十周年”慶祝活動,并在當天發布新版本 1.87.0。
新版本的主要新特性包括:
標準庫加入匿名管道(Anonymous Pipes)
use std::io::Read; let (mut recv, send) = std::io::pipe()?; letmut command = Command::new("path/to/bin") // Both stdout and stderr will write to the same pipe, combining the two. .stdout(send.try_clone()?) .stderr(send) .spawn()?; letmut output = Vec::new(); recv.read_to_end(&mut output)?; // It's important that we read from the pipe before the process exits, to avoid // filling the OS buffers if the program emits too much output. assert!(command.wait()?.success());
安全架構 intrinsics
#![forbid(unsafe_op_in_unsafe_fn)] use std::arch::x86_64::*; fnsum(slice: &[u32]) -> u32 { #[cfg(target_arch = "x86_64")] { if is_x86_feature_detected!("avx2") { // SAFETY: We have detected the feature is enabled at runtime, // so it's safe to call this function. returnunsafe { sum_avx2(slice) }; } } slice.iter().sum() } #[target_feature(enable = "avx2")] #[cfg(target_arch = "x86_64")] fnsum_avx2(slice: &[u32]) -> u32 { // SAFETY: __m256i and u32 have the same validity. let (prefix, middle, tail) = unsafe { slice.align_to::<__m256i>() }; letmut sum = prefix.iter().sum::
(); sum += tail.iter().sum::
(); // Core loop is now fully safe code in 1.87, because the intrinsics require // matching target features (avx2) to the function definition. letmut base = _mm256_setzero_si256(); for e in middle.iter() { base = _mm256_add_epi32(base, *e); } // SAFETY: __m256i and u32 have the same validity. let base: [u32; 8] = unsafe { std::mem::transmute(base) }; sum += base.iter().sum::
(); sum }
通過
asm!
內聯匯編可跳轉到 Rust 代碼中的標記塊
unsafe { asm!( "jmp {}", label { println!("Jumped from asm!"); } ); }
穩定 API 等等
為慶祝 Rust 1.0 穩定版發布十周年,Rust 作者 Graydon Hoare 寫了一篇《10 Years of Stable Rust: An Infrastructure Story》長文進行回顧,他在文章提到了一組數據:
1.0 之前,Rust 代碼庫記錄了 4 萬次變更;此后又新增了 24.6 萬次提交。換算下來,過去 10 年幾乎是每小時合并 2.8 次提交。
貢獻者從 1.0 時不足 1000 人,發展到現在約 6700 人
項目已關閉超過 4.7 萬個 issue,處理了 14 萬多個 PR
1.0 時共計約 1100 份 RFC(用于語言演進的提案),如今累計達到了 3772 份
自 1.0 以來發布了 87 個正式版本,大多數都按六周節奏準時發布
推出過 3 個 Edition(版本變更打包,兼容舊代碼),用于引入需要 opt-in 的非兼容變更
每個版本的兼容性測試范圍從 2500 個 crate 增長到了現在的 58.7 萬個
話說當初創建 Rust 語言,據傳是 Graydon Hoare 回家后發現電梯壞了,自己得爬到 21 樓上。爬樓的時候就在瞎琢磨電梯故障很可能是 C/C++ 引發的內存軟件故障,越想越氣,爬上去就決定要搞個新的內存安全的語言出來。
小編把上面這段文字發給 AI 判定真實性,得到了如下回復:
這段內容并非完全虛假杜撰,是有一定真實背景的。Rust 編程語言的誕生確實與 Graydon Hoare 的一次電梯故障經歷有關,但具體的細節在流傳過程中可能有所夸張或演繹。
2006 年,Graydon Hoare 在 Mozilla 公司工作,住在溫哥華的公寓 21 樓。某天他下班回家發現電梯又壞了,只能爬樓梯上去。他很清楚,電梯軟件大多是用 C 或 C++ 編寫的,而這類語言容易出現內存管理問題導致軟件崩潰。
于是便萌生了設計一門新編程語言的想法,希望這種語言既能寫出小而快的程序,又能從根本上避免內存錯誤等常見問題,Rust 語言由此誕生。
不過,Rust 語言的創建并非僅因這一偶然事件,Graydon Hoare 當時對 C/C++ 等語言在內存安全、并發等方面的不足已有深刻認識,也一直希望探索一種更好的編程語言。
而且,Rust 從最初構想到最終成型經歷了漫長過程,背后還有 Mozilla 等眾多團隊和個人的共同努力。
↓分享、在看與點贊~Orz
特別聲明:以上內容(如有圖片或視頻亦包括在內)為自媒體平臺“網易號”用戶上傳并發布,本平臺僅提供信息存儲服務。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.