fbpx
维基百科

Rust

Rust是由Mozilla[11]主导开发的通用、编译型编程语言。设计准则为“安全、并发、实用”,[12][13]支持函数式並行式过程式以及面向对象的程式設計风格。

Rust
编程范型編譯語言並行计算
函数式指令式
物件導向结构化
設計者Graydon Hoare
實作者Mozilla
发行时间2010年
目前版本
  • 1.67.1 (2023年2月9日;穩定版本)[1]
型態系統靜態類型強型別
類型推論結構類型英语Structural type system
作業系統LinuxmacOSWindows
FreeBSDAndroidiOS[2]
許可證Apache许可证2.0及MIT許可證[3]
文件扩展名.rs、.rlib
網站rust-lang.org
啟發語言
Alef英语Alef (programming language)[4]C#[4]C++[4]Cyclone英语Cyclone (programming language)[4][5]
Erlang[4]Haskell[4]Hermes英语Hermes (programming language)[4]Limbo[4]
Newsqueak[4]NIL英语NIL (programming language)[4]OCaml[4]Ruby[4]
Scheme[4]Standard ML[4]Swift[4][6]
影響語言
C# 7[7]Elm[8]Idris[9]Swift[10]

Rust語言原本是Mozilla員工Graydon Hoare的私人計劃,而Mozilla於2009年開始贊助這個計劃 [14],並且在2010年首次公開[15]。也在同一年,其編譯器原始碼開始由原本的OCaml語言轉移到用Rust語言,進行自舉英语Bootstrapping (compilers)工作,稱做「rustc」[16],並於2011年實際完成[17]。這個可自我編譯的編譯器在架構上採用了LLVM做為它的後端。

第一個有版本號的Rust編譯器於2012年1月釋出。[18]Rust 1.0是第一個穩定版本,於2015年5月15日釋出。[19]

Rust在完全開放的情況下開發,並且相當歡迎社区的回饋。在1.0穩定版之前,語言設計也因為透過撰寫Servo網頁瀏覽器排版引擎和rustc編譯器本身,而有進一步的改善。它雖然由Mozilla資助,但其實是一個共有專案,有很大部分的程式碼是來自於社区的貢獻者。[20]

設計

Rust的設計目標之一,是要使設計大型的網際網路客户端伺服器的任務變得更容易[21]。因此更加強調安全性、記憶體配置、以及並行處理等方面的特性。

性能

在效能上,具有額外安全保證的代碼會比C++慢一些,例如Rust对数组进行操作时会進行边界检查(尽管可以通过一些方式[22]绕过[23]),而C++则不会,但是如果等價的C++代碼作手工检查,則兩者效能上是相似的[24]

编译报错

比起C/C++,Rust编译器的对于代码中错误的提示更清晰明瞭,开发者可根据提示轻松地修复代码中的错误。

編譯速度

由於其編譯器會做出額外的安全檢查,Rust的編譯速度有時低於C/C++。

语法

Rust的語法設計,與C語言C++相當相似,區塊(block)使用大括號隔開,流程控制的關鍵字如ifelsewhile等等。在保持相似性的同時,Rust也加進了新的關鍵字,如用於模式匹配(pattern matching)的match(與switch相似)則是使用C/C++系統程式語言的人會相對陌生的概念。儘管在語法上相似,Rust的語義(semantic)和C/C++非常不同。

内存安全

為了提供内存安全,它的設計不允許空指標懸空指標[25][26]。 指針只能透過固定的初始化形態來建構,而所有這些形態都要求它們的輸入已經分析過了[27]。Rust有一個檢查指標生命期間和指標凍結的系統,可以用來預防在C++中許多的型別錯誤,甚至是用了智能指针功能之後會發生的型別錯誤。

所有权

Rust设计了一个所有权系统,其中所有值都有一个唯一的所有者,并且值的作用域与所有者的作用域相同。值可以通过不可变引用(&T)、可变引用(&mut T)或者通过值本身(T)传递。任何时候,一个变量都可以有多个不可变引用或一个可变引用,这实际上是一个显式的读写锁。Rust编译器在编译时强制执行这些规则,并检查所有引用是否有效。[28][29]

内存管理

早期的Rust雖然有垃圾回收系統,但非如Java.NET平台的全自動垃圾回收。Rust 1.0已不再使用垃圾回收器,而是全面改用基于引用计数的智能指针来管理内存。

类型与多态

它的型別系統直接地模仿了Haskell語言的类型类概念,並把它稱作「traits」,可以把它看成是一種特设多态。Rust的作法是透過在宣告型別變數(type variable)的時候,在上面加上限制條件。至於Haskell的高階型別變數(Higher-kinded polymorphism)則還未支援。

型別推導也是Rust提供的特性之一,使用let語法宣告的變數可以不用宣告型別,亦不需要初始值來推斷型別。但如果在稍後的程式中從未指派任何值到該變數,編譯器會發出編譯時(compile time)錯誤[30]。 函數可以使用泛型化參數(generics),但是必須綁定Trait。不能使用方法或運算子而不声明它們的型別,每一項都必確明確定義。

Rust的物件系統是基於三樣東西之上的,即實作(implementation)、Trait以及結構化資料(如struct)。實作的角色類似提供Class關鍵字的程式語言所代表的意義,並使用impl關鍵字。繼承和多型則通过Trait實現,它們使得方法(method)可以在實作中被定義。結構化資料用來定義欄位。實作和(trait)都無法定義欄位,並且只有(trait)可以提供繼承,藉以躲避C++的「鑽石繼承問題」(菱型缺陷)。

历史

2006年,Rust作为Graydon Hoare的个人项目首次出现。

2009年,Graydon Hoare成为Mozilla雇员[14]

2010年,Rust首次作为Mozilla官方项目出现[15]。同年,Rust开始从初始编译(由OCaml写成)转变为自编译[16]

2011年,Rust成功的完成了移植[17]。Rust的自编译器采用LLVM作为其编译后端。

2012年1月20日,第一个有版本号的预览版Rust编译器发布[18]

2013年4月4日,Mozilla基金會宣布將與三星集團合作開發瀏覽器排版引擎Servo,此引擎将由Rust來實作[31]

2015年5月16日,Rust 1.0.0发布[32]

2020年3月27日,Rust核心团队成员Steve Klabnik在官方博客发表了一篇名为《Goodbye, docs team 》的文章,叙述了Rust文档的现状[33]

2021年2月8日,AWS華為Google微軟以及Mozilla宣布成立Rust基金會[34][35]

2022年9月19日,Linux初始開發者林纳斯·托瓦兹表示在Linux核心6.1版中會有對Rust的初步支援[36]

生态系统

除了编译器和标准库,Rust生态系统还包括用于软件开发的额外组件。官方推荐使用Rustup,一个Rust工具链安装程序来管理这些组件。

Cargo

Cargo是Rust的软件包管理器,用来下载和构建依赖关系。Cargo还充当了Clippy和其他Rust组件的封装器。它要求项目遵循一定的目录结构。[37]

Cargo.toml文件指定了项目所需的依赖和版本要求,告诉Cargo哪些版本的依赖关系与该包兼容。Cargo默认从crates.io中获取依赖,但Git仓库和本地文件系统中的包也可以作为依赖。[38]

代码示例

下面的代码在Rust 1.3中测试通过。

Hello World

fn main() {  println!("Hello, World!"); } 

如果不想使输出包含换行符(\n),可以使用print!巨集代替println!巨集。

階乘

下面是三個不同版本的階乘函數,分別以遞迴、迴圈和反覆運算器的方法寫成:

// 這個函數的if-else語句中展示了Rust中可選的隱式返回值,可用於寫出更像函數式程式設計風格的代碼 // 與C++和其他類似的語言不同,Rust中的if-else結構不是語句而是運算式,有返回值 fn recursive_factorial(n: u32) -> u32 {  if n <= 1 {  1  } else {  n * recursive_factorial(n - 1)  } } fn iterative_factorial(n: u32) -> u32 {  // 變數用`let`定義,`mut`關鍵字使得變數可以變化  let mut i = 1u32;  let mut result = 1u32;  while i <= n {  result *= i;  i += 1;  }  result // 顯式返回值,與上一個函數不同 } fn iterator_factorial(n: u32) -> u32 {  // 反覆運算器有多種用於變換的函數  // |accum, x| 定義了一個匿名函數  // 內聯展開等優化方法會消去區間和fold,使本函數的運行效率和上一個函數相近  (1..n + 1).fold(1, |accum, x| accum * x) } fn main() {  println!("Recursive result: {}", recursive_factorial(10));  println!("Iterative result: {}", iterative_factorial(10));  println!("Iterator result: {}", iterator_factorial(10)); } 

併行

一個簡單的Rust併發示例:

use std::thread; // 這個函數將創建十個同時併發運行的執行緒 // 若要驗證這一點,可多次運行這個程式,觀察各執行緒輸出順序的隨機性 fn main() {  // 這個字串是不可變的,因此可以安全地同時被多個執行緒訪問  let greeting = "Hello";  let mut threads = Vec::new();  // `for`迴圈可用於任何實現了`iterator`特性的類型  for num in 0..10 {  threads.push(thread::spawn(move || {  // `println!`是一個可以靜態檢查格式字串類型的巨集  // Rust的巨集是基於結構的(如同Scheme)而不是基於文本的(如同C)  println!("{} from thread number {}", greeting, num);  }));  }  // 收集所有執行緒,保證它們在程式退出前全部結束  for thread in threads {  thread.join().unwrap();  } } 

參考資料

  1. ^ 1.0 1.1 Announcing Rust 1.67.1. 
  2. ^ . [2017-03-17]. (原始内容存档于2018-02-13). 
  3. ^ COPYRIGHT. Rust compiler source repository. [2012-12-17]. 
  4. ^ 4.00 4.01 4.02 4.03 4.04 4.05 4.06 4.07 4.08 4.09 4.10 4.11 4.12 4.13 4.14 The Rust Reference: Appendix: Influences. [2015-03-25]. (原始内容于2015-03-26). Rust is not a particularly original language, with design elements coming from a wide range of sources. Some of these are listed below (including elements that have since been removed): SML, OCaml [...] C++ [...] ML Kit, Cyclone [...] Haskell [...] Newsqueak, Alef, Limbo [...] Erlang [...] Swift [...] Scheme [...] C# [...] Ruby [...] NIL, Hermes 
  5. ^ Note Research: Type System. 2015-02-01 [2015-03-25]. (原始内容于2019-02-17). Papers that have had more or less influence on Rust, or which one might want to consult for inspiration or to understand Rust's background. [...] Region based memory management in Cyclone [...] Safe memory management in Cyclone 
  6. ^ RFC for `if let` expression. [2014-12-04]. (原始内容于2016-03-04). The `if let` construct is based on the precedent set by Swift, which introduced its own `if let` statement. 
  7. ^ Discussion - Patterns and Records. 2015-03-25 [2015-03-25]. (原始内容于2015-08-19). Sources of Inspiration: [...] Rust 
  8. ^ Command Optimizations?. 2014-06-26 [2014-12-10]. (原始内容于2019-07-08). I just added the outline of a Result library that lets you use richer error messages. It's like Either except the names are more helpful. The names are inspired by Rust's Result library. 
  9. ^ . 2014-08-22 [2014-10-27]. (原始内容存档于2014-12-25). They are inspired by linear types, Uniqueness Types in the Clean programming language, and ownership types and borrowed pointers in the Rust programming language. 
  10. ^ Lattner, Chris. Chris Lattner's Homepage. Chris Lattner. 2014-06-03 [2014-06-03]. (原始内容于2018-12-25). The Swift language is the product of tireless effort from a team of language experts, documentation gurus, compiler optimization ninjas, and an incredibly important internal dogfooding group who provided feedback to help refine and battle-test ideas. Of course, it also greatly benefited from the experiences hard-won by many other languages in the field, drawing ideas from Objective-C, Rust, Haskell, Ruby, Python, C#, CLU, and far too many others to list. 
  11. ^ Noel. The Rust Language. Lambda the Ultimate. 2010-07-08 [2010-10-30]. (原始内容于2015-11-18). 
  12. ^ The Rust Programming Language. [2012-10-21]. (原始内容于2016-06-18). 
  13. ^ Doc language FAQ. [2012-10-21]. (原始内容于2020-07-20). 
  14. ^ 14.0 14.1 Project FAQ. 2010-09-14 [2012-01-11]. (原始内容于2020-07-20). 
  15. ^ 15.0 15.1 Future Tense. 2011-04-29 [2012-02-06]. (原始内容存档于2012-09-18). At Mozilla Summit 2010, we launched Rust, a new programming language motivated by safety and concurrency for parallel hardware, the “manycore” future which is upon us. 
  16. ^ 16.0 16.1 Hoare, Graydon. . 2010-10-02 [2010-10-30]. (原始内容存档于2014-08-15). 
  17. ^ 17.0 17.1 Hoare, Graydon. [rust-dev] stage1/rustc builds. 2011-04-20 [2011-04-20]. (原始内容于2011-07-20). After that last change fixing the logging scope context bug, looks like stage1/rustc builds. Just shy of midnight :) 
  18. ^ 18.0 18.1 catamorphism. Mozilla and the Rust community release Rust 0.1 (a strongly-typed systems programming language with a focus on memory safety and concurrency). 2012-01-20 [2012-02-06]. (原始内容于2012-01-24). 
  19. ^ rust/RELEASES.md at master · rust-lang/rust · GitHub. GitHub. [2015-07-26]. (原始内容于2015-05-15). 
  20. ^ Rust Contributors. [2015-05-17]. (原始内容于2020-05-26). 
  21. ^ Avram, Abel. Interview on Rust, a Systems Programming Language Developed by Mozilla. InfoQ. 2012-08-03 [2013-08-17]. (原始内容于2013-07-24). GH:在其他程式語言中有很多好的、討人喜愛的想法,沒有被廣泛採用的系統程式語言採納...在1970和80年代時,有些相當不錯的競爭者同時存在,我想要把它們所有的某些特性再拿出來,因為現況已經改變:網際網路是併發性非常強的系統,也更加重視安全性,所以以往獨鍾C以及C++的優勢不再成立。 
  22. ^ . The Rust Programming Language Forum. 2016-01-26 [2021-02-08]. (原始内容存档于2021-02-08) (英语). 
  23. ^ 范, 范长春. 数组和字符串. 深入浅出Rust 2018年8月第一版. 北京: 机械工业出版社. 2018: 72-73. ISBN 9787111606420. OCLC 1097888310. 
  24. ^ Walton, Patrick. C++ Design Goals in the Context of Rust. 2010-12-05 [2011-01-21]. (原始内容于2010-12-09). ...想要維持和C一樣快又同時保證安全是不可能的...C++在設計上允許各種低層操作,大部分與迴避型態系統有關,藉此C++有了幾乎無限制的最佳化能力。不過在實際上,C++工程師在自己的程式碼中只使用某些特定的工具或技巧,例如透過pass by alias傳遞stack變數、獨特擁有的物件(通常是auto_ptr或C++0x的unique_ptr)、使用shared_ptr來達成「引用計數」、COM等等。Rust的型態系統的設計目標之一,就是在語言中融入這些安全性設計,並且強迫實行這些原則。這樣的話,效能可以與C++比較,又能同時保持記憶體安全... 
  25. ^ Rosenblatt, Seth. Samsung joins Mozilla's quest for Rust. 2013-04-03 [2013-04-05]. (原始内容于2013-04-04). [Brendan Eich]提出,每一年,瀏覽器都會在Pwn2Own競賽上發現新的漏洞。他說,Rust「不允許自由讀取記憶體」,但C++則可以。這些便是「導致瀏覽器弱點」,也是能夠自編譯的Rust要解決的問題。 
  26. ^ Brown, Neil. A taste of Rust. 2013-04-17 [2013-04-25]. (原始内容于2013-04-26). ...當然,為了更大程度的記憶體分享,使用者可以實作更複雜的資料結構,並同時保持介面只由被擁有和被管理的參照所組成。如此便解決了競爭存取和懸空指標的問題。 
  27. ^ Language FAQ. 2015-05-17 [2015-05-17]. (原始内容于2015-05-15). 
  28. ^ Klabnik, Steve; Nichols, Carol. Chapter 4: Understanding Ownership. The Rust Programming Language. San Francisco, California: No Starch Press. June 2018: 44 [2019-05-14]. ISBN 978-1-593-27828-1. (原始内容于2019-05-03) (英语). 
  29. ^ The Rust Programming Language: What is Ownership. Rust-lang.org. [2019-05-14]. (原始内容于2019-05-19) (英语). 
  30. ^ Walton, Patrick. Rust Features I: Type Inference. 2010-10-01 [2011-01-21]. (原始内容于2011-07-08). 
  31. ^ Peter Bright. Samsung teams up with Mozilla to build browser engine for multicore machines. 2013-04-03 [2013-04-04]. (原始内容于2016-12-16). 
  32. ^ The Rust Core Team. Announcing Rust 1.0. May 15, 2015 [2015-12-11]. (原始内容于2015-05-15). 
  33. ^ Goodbye, docs team. [2022-10-10]. (原始内容于2022-12-01) (英语). 
  34. ^ Rust Foundation. foundation.rust-lang.org. 2021-02-08 [2021-02-09]. (原始内容于2021-02-09) (英语). 
  35. ^ Mozilla Welcomes the Rust Foundation. Mozilla Blog. 2021-02-09 [2021-02-09]. (原始内容于2021-02-08) (美国英语). 
  36. ^ Linus Torvalds: Rust will go into Linux 6.1. ZDNET. [2022-10-06]. (原始内容于2023-01-19) (英语). 
  37. ^ . doc.rust-lang.org. [2022-02-01]. (原始内容存档于2022-04-07). 
  38. ^ . doc.rust-lang.org. [2022-02-01]. (原始内容存档于2022-04-07). 

参閱

外部链接

rust, 此條目介紹的是由mozilla主导开发的编程语言, 关于, rust, 在英文中的本意, 请见, 铁锈, 关于由facepunch工作室所開發的一款游戲, 请见, 腐蚀, 游戏, 是由mozilla, 主导开发的通用, 编译型编程语言, 设计准则为, 安全, 并发, 实用, 支持函数式, 並行式, 过程式以及面向对象的程式設計风格, 编程范型編譯語言, 並行计算, 函数式, 指令式, 物件導向, 结构化設計者graydon, hoare實作者mozilla发行时间2010年目前版本1, 2023年2月9. 此條目介紹的是由Mozilla主导开发的编程语言 关于 rust 在英文中的本意 请见 铁锈 关于由Facepunch工作室所開發的一款游戲 请见 腐蚀 游戏 Rust是由Mozilla 11 主导开发的通用 编译型编程语言 设计准则为 安全 并发 实用 12 13 支持函数式 並行式 过程式以及面向对象的程式設計风格 Rust编程范型編譯語言 並行计算 函数式 指令式 物件導向 结构化設計者Graydon Hoare實作者Mozilla发行时间2010年目前版本1 67 1 2023年2月9日 穩定版本 1 型態系統靜態類型 強型別 類型推論 結構類型 英语 Structural type system 作業系統Linux macOS WindowsFreeBSD Android iOS等 2 許可證Apache许可证2 0及MIT許可證 3 文件扩展名 rs rlib網站rust lang org啟發語言Alef 英语 Alef programming language 4 C 4 C 4 Cyclone 英语 Cyclone programming language 4 5 Erlang 4 Haskell 4 Hermes 英语 Hermes programming language 4 Limbo 4 Newsqueak 4 NIL 英语 NIL programming language 4 OCaml 4 Ruby 4 Scheme 4 Standard ML 4 Swift 4 6 影響語言C 7 7 Elm 8 Idris 9 Swift 10 Rust語言原本是Mozilla員工Graydon Hoare的私人計劃 而Mozilla於2009年開始贊助這個計劃 14 並且在2010年首次公開 15 也在同一年 其編譯器原始碼開始由原本的OCaml語言轉移到用Rust語言 進行自舉 英语 Bootstrapping compilers 工作 稱做 rustc 16 並於2011年實際完成 17 這個可自我編譯的編譯器在架構上採用了LLVM做為它的後端 第一個有版本號的Rust編譯器於2012年1月釋出 18 Rust 1 0是第一個穩定版本 於2015年5月15日釋出 19 Rust在完全開放的情況下開發 並且相當歡迎社区的回饋 在1 0穩定版之前 語言設計也因為透過撰寫Servo網頁瀏覽器排版引擎和rustc編譯器本身 而有進一步的改善 它雖然由Mozilla資助 但其實是一個共有專案 有很大部分的程式碼是來自於社区的貢獻者 20 目录 1 設計 1 1 性能 1 2 编译报错 1 3 編譯速度 1 4 语法 1 5 内存安全 1 6 所有权 1 7 内存管理 1 8 类型与多态 2 历史 3 生态系统 3 1 Cargo 4 代码示例 4 1 Hello World 4 2 階乘 4 3 併行 5 參考資料 6 参閱 7 外部链接設計 编辑Rust的設計目標之一 是要使設計大型的網際網路客户端和伺服器的任務變得更容易 21 因此更加強調安全性 記憶體配置 以及並行處理等方面的特性 性能 编辑 在效能上 具有額外安全保證的代碼會比C 慢一些 例如Rust对数组进行操作时会進行边界检查 尽管可以通过一些方式 22 绕过 23 而C 则不会 但是如果等價的C 代碼作手工检查 則兩者效能上是相似的 24 编译报错 编辑 比起C C Rust编译器的对于代码中错误的提示更清晰明瞭 开发者可根据提示轻松地修复代码中的错误 編譯速度 编辑 由於其編譯器會做出額外的安全檢查 Rust的編譯速度有時低於C C 语法 编辑 Rust的語法設計 與C語言和C 相當相似 區塊 block 使用大括號隔開 流程控制的關鍵字如if else while等等 在保持相似性的同時 Rust也加進了新的關鍵字 如用於模式匹配 pattern matching 的match 與switch相似 則是使用C C 系統程式語言的人會相對陌生的概念 儘管在語法上相似 Rust的語義 semantic 和C C 非常不同 内存安全 编辑 為了提供内存安全 它的設計不允許空指標和懸空指標 25 26 指針只能透過固定的初始化形態來建構 而所有這些形態都要求它們的輸入已經分析過了 27 Rust有一個檢查指標生命期間和指標凍結的系統 可以用來預防在C 中許多的型別錯誤 甚至是用了智能指针功能之後會發生的型別錯誤 所有权 编辑 Rust设计了一个所有权系统 其中所有值都有一个唯一的所有者 并且值的作用域与所有者的作用域相同 值可以通过不可变引用 amp T 可变引用 amp mut T 或者通过值本身 T 传递 任何时候 一个变量都可以有多个不可变引用或一个可变引用 这实际上是一个显式的读写锁 Rust编译器在编译时强制执行这些规则 并检查所有引用是否有效 28 29 内存管理 编辑 早期的Rust雖然有垃圾回收系統 但非如Java或 NET平台的全自動垃圾回收 Rust 1 0已不再使用垃圾回收器 而是全面改用基于引用计数的智能指针来管理内存 类型与多态 编辑 它的型別系統直接地模仿了Haskell語言的类型类概念 並把它稱作 traits 可以把它看成是一種特设多态 Rust的作法是透過在宣告型別變數 type variable 的時候 在上面加上限制條件 至於Haskell的高階型別變數 Higher kinded polymorphism 則還未支援 型別推導也是Rust提供的特性之一 使用let語法宣告的變數可以不用宣告型別 亦不需要初始值來推斷型別 但如果在稍後的程式中從未指派任何值到該變數 編譯器會發出編譯時 compile time 錯誤 30 函數可以使用泛型化參數 generics 但是必須綁定Trait 不能使用方法或運算子而不声明它們的型別 每一項都必確明確定義 Rust的物件系統是基於三樣東西之上的 即實作 implementation Trait以及結構化資料 如struct 實作的角色類似提供Class關鍵字的程式語言所代表的意義 並使用impl關鍵字 繼承和多型則通过Trait實現 它們使得方法 method 可以在實作中被定義 結構化資料用來定義欄位 實作和 trait 都無法定義欄位 並且只有 trait 可以提供繼承 藉以躲避C 的 鑽石繼承問題 菱型缺陷 历史 编辑2006年 Rust作为Graydon Hoare的个人项目首次出现 2009年 Graydon Hoare成为Mozilla雇员 14 2010年 Rust首次作为Mozilla官方项目出现 15 同年 Rust开始从初始编译 由OCaml写成 转变为自编译 16 2011年 Rust成功的完成了移植 17 Rust的自编译器采用LLVM作为其编译后端 2012年1月20日 第一个有版本号的预览版Rust编译器发布 18 2013年4月4日 Mozilla基金會宣布將與三星集團合作開發瀏覽器排版引擎Servo 此引擎将由Rust來實作 31 2015年5月16日 Rust 1 0 0发布 32 2020年3月27日 Rust核心团队成员Steve Klabnik在官方博客发表了一篇名为 Goodbye docs team 的文章 叙述了Rust文档的现状 33 2021年2月8日 AWS 華為 Google 微軟以及Mozilla宣布成立Rust基金會 34 35 2022年9月19日 Linux初始開發者林纳斯 托瓦兹表示在Linux核心6 1版中會有對Rust的初步支援 36 生态系统 编辑除了编译器和标准库 Rust生态系统还包括用于软件开发的额外组件 官方推荐使用Rustup 一个Rust工具链安装程序来管理这些组件 Cargo 编辑 Cargo是Rust的软件包管理器 用来下载和构建依赖关系 Cargo还充当了Clippy和其他Rust组件的封装器 它要求项目遵循一定的目录结构 37 Cargo toml文件指定了项目所需的依赖和版本要求 告诉Cargo哪些版本的依赖关系与该包兼容 Cargo默认从crates io中获取依赖 但Git仓库和本地文件系统中的包也可以作为依赖 38 代码示例 编辑下面的代码在Rust 1 3中测试通过 Hello World 编辑 fn main println Hello World 如果不想使输出包含换行符 n 可以使用print 巨集代替println 巨集 階乘 编辑 下面是三個不同版本的階乘函數 分別以遞迴 迴圈和反覆運算器的方法寫成 這個函數的if else語句中展示了Rust中可選的隱式返回值 可用於寫出更像函數式程式設計風格的代碼 與C 和其他類似的語言不同 Rust中的if else結構不是語句而是運算式 有返回值 fn recursive factorial n u32 gt u32 if n lt 1 1 else n recursive factorial n 1 fn iterative factorial n u32 gt u32 變數用 let 定義 mut 關鍵字使得變數可以變化 let mut i 1 u32 let mut result 1 u32 while i lt n result i i 1 result 顯式返回值 與上一個函數不同 fn iterator factorial n u32 gt u32 反覆運算器有多種用於變換的函數 accum x 定義了一個匿名函數 內聯展開等優化方法會消去區間和fold 使本函數的運行效率和上一個函數相近 1 n 1 fold 1 accum x accum x fn main println Recursive result recursive factorial 10 println Iterative result iterative factorial 10 println Iterator result iterator factorial 10 併行 编辑 一個簡單的Rust併發示例 use std thread 這個函數將創建十個同時併發運行的執行緒 若要驗證這一點 可多次運行這個程式 觀察各執行緒輸出順序的隨機性 fn main 這個字串是不可變的 因此可以安全地同時被多個執行緒訪問 let greeting Hello let mut threads Vec new for 迴圈可用於任何實現了 iterator 特性的類型 for num in 0 10 threads push thread spawn move println 是一個可以靜態檢查格式字串類型的巨集 Rust的巨集是基於結構的 如同Scheme 而不是基於文本的 如同C println from thread number greeting num 收集所有執行緒 保證它們在程式退出前全部結束 for thread in threads thread join unwrap 參考資料 编辑 1 0 1 1 Announcing Rust 1 67 1 Rust Platform Support 2017 03 17 原始内容存档于2018 02 13 COPYRIGHT Rust compiler source repository 2012 12 17 4 00 4 01 4 02 4 03 4 04 4 05 4 06 4 07 4 08 4 09 4 10 4 11 4 12 4 13 4 14 The Rust Reference Appendix Influences 2015 03 25 原始内容存档于2015 03 26 Rust is not a particularly original language with design elements coming from a wide range of sources Some of these are listed below including elements that have since been removed SML OCaml C ML Kit Cyclone Haskell Newsqueak Alef Limbo Erlang Swift Scheme C Ruby NIL Hermes Note Research Type System 2015 02 01 2015 03 25 原始内容存档于2019 02 17 Papers that have had more or less influence on Rust or which one might want to consult for inspiration or to understand Rust s background Region based memory management in Cyclone Safe memory management in Cyclone RFC for if let expression 2014 12 04 原始内容存档于2016 03 04 The if let construct is based on the precedent set by Swift which introduced its own if let statement Discussion Patterns and Records 2015 03 25 2015 03 25 原始内容存档于2015 08 19 Sources of Inspiration Rust Command Optimizations 2014 06 26 2014 12 10 原始内容存档于2019 07 08 I just added the outline of a Result library that lets you use richer error messages It s like Either except the names are more helpful The names are inspired by Rust s Result library Uniqueness Types 2014 08 22 2014 10 27 原始内容存档于2014 12 25 They are inspired by linear types Uniqueness Types in the Clean programming language and ownership types and borrowed pointers in the Rust programming language Lattner Chris Chris Lattner s Homepage Chris Lattner 2014 06 03 2014 06 03 原始内容存档于2018 12 25 The Swift language is the product of tireless effort from a team of language experts documentation gurus compiler optimization ninjas and an incredibly important internal dogfooding group who provided feedback to help refine and battle test ideas Of course it also greatly benefited from the experiences hard won by many other languages in the field drawing ideas from Objective C Rust Haskell Ruby Python C CLU and far too many others to list Noel The Rust Language Lambda the Ultimate 2010 07 08 2010 10 30 原始内容存档于2015 11 18 The Rust Programming Language 2012 10 21 原始内容存档于2016 06 18 Doc language FAQ 2012 10 21 原始内容存档于2020 07 20 14 0 14 1 Project FAQ 2010 09 14 2012 01 11 原始内容存档于2020 07 20 15 0 15 1 Future Tense 2011 04 29 2012 02 06 原始内容存档于2012 09 18 At Mozilla Summit 2010 we launched Rust a new programming language motivated by safety and concurrency for parallel hardware the manycore future which is upon us 16 0 16 1 Hoare Graydon Rust Progress 2010 10 02 2010 10 30 原始内容存档于2014 08 15 17 0 17 1 Hoare Graydon rust dev stage1 rustc builds 2011 04 20 2011 04 20 原始内容存档于2011 07 20 After that last change fixing the logging scope context bug looks like stage1 rustc builds Just shy of midnight 18 0 18 1 catamorphism Mozilla and the Rust community release Rust 0 1 a strongly typed systems programming language with a focus on memory safety and concurrency 2012 01 20 2012 02 06 原始内容存档于2012 01 24 rust RELEASES md at master rust lang rust GitHub GitHub 2015 07 26 原始内容存档于2015 05 15 Rust Contributors 2015 05 17 原始内容存档于2020 05 26 Avram Abel Interview on Rust a Systems Programming Language Developed by Mozilla InfoQ 2012 08 03 2013 08 17 原始内容存档于2013 07 24 GH 在其他程式語言中有很多好的 討人喜愛的想法 沒有被廣泛採用的系統程式語言採納 在1970和80年代時 有些相當不錯的競爭者同時存在 我想要把它們所有的某些特性再拿出來 因為現況已經改變 網際網路是併發性非常強的系統 也更加重視安全性 所以以往獨鍾C以及C 的優勢不再成立 How to avoid bounds checking The Rust Programming Language Forum 2016 01 26 2021 02 08 原始内容存档于2021 02 08 英语 范 范长春 数组和字符串 深入浅出Rust 2018年8月第一版 北京 机械工业出版社 2018 72 73 ISBN 9787111606420 OCLC 1097888310 Walton Patrick C Design Goals in the Context of Rust 2010 12 05 2011 01 21 原始内容存档于2010 12 09 想要維持和C一樣快又同時保證安全是不可能的 C 在設計上允許各種低層操作 大部分與迴避型態系統有關 藉此C 有了幾乎無限制的最佳化能力 不過在實際上 C 工程師在自己的程式碼中只使用某些特定的工具或技巧 例如透過pass by alias傳遞stack變數 獨特擁有的物件 通常是auto ptr或C 0x的unique ptr 使用shared ptr來達成 引用計數 COM等等 Rust的型態系統的設計目標之一 就是在語言中融入這些安全性設計 並且強迫實行這些原則 這樣的話 效能可以與C 比較 又能同時保持記憶體安全 Rosenblatt Seth Samsung joins Mozilla s quest for Rust 2013 04 03 2013 04 05 原始内容存档于2013 04 04 Brendan Eich 提出 每一年 瀏覽器都會在Pwn2Own競賽上發現新的漏洞 他說 Rust 不允許自由讀取記憶體 但C 則可以 這些便是 導致瀏覽器弱點 也是能夠自編譯的Rust要解決的問題 Brown Neil A taste of Rust 2013 04 17 2013 04 25 原始内容存档于2013 04 26 當然 為了更大程度的記憶體分享 使用者可以實作更複雜的資料結構 並同時保持介面只由被擁有和被管理的參照所組成 如此便解決了競爭存取和懸空指標的問題 Language FAQ 2015 05 17 2015 05 17 原始内容存档于2015 05 15 Klabnik Steve Nichols Carol Chapter 4 Understanding Ownership The Rust Programming Language San Francisco California No Starch Press June 2018 44 2019 05 14 ISBN 978 1 593 27828 1 原始内容存档于2019 05 03 英语 The Rust Programming Language What is Ownership Rust lang org 2019 05 14 原始内容存档于2019 05 19 英语 Walton Patrick Rust Features I Type Inference 2010 10 01 2011 01 21 原始内容存档于2011 07 08 Peter Bright Samsung teams up with Mozilla to build browser engine for multicore machines 2013 04 03 2013 04 04 原始内容存档于2016 12 16 The Rust Core Team Announcing Rust 1 0 May 15 2015 2015 12 11 原始内容存档于2015 05 15 Goodbye docs team 2022 10 10 原始内容存档于2022 12 01 英语 Rust Foundation foundation rust lang org 2021 02 08 2021 02 09 原始内容存档于2021 02 09 英语 Mozilla Welcomes the Rust Foundation Mozilla Blog 2021 02 09 2021 02 09 原始内容存档于2021 02 08 美国英语 Linus Torvalds Rust will go into Linux 6 1 ZDNET 2022 10 06 原始内容存档于2023 01 19 英语 Why Cargo Exists The Cargo Book doc rust lang org 2022 02 01 原始内容存档于2022 04 07 Specifying Dependencies The Cargo Book doc rust lang org 2022 02 01 原始内容存档于2022 04 07 参閱 编辑Servo外部链接 编辑官方网站 页面存档备份 存于互联网档案馆 简体中文 页面存档备份 存于互联网档案馆 正體中文 页面存档备份 存于互联网档案馆 The Rust dev Archives 页面存档备份 存于互联网档案馆 邮件列表 Rust主源代码库及缺陷跟踪系统 页面存档备份 存于互联网档案馆 于Github Rust语言Wiki 页面存档备份 存于互联网档案馆 于Github Rust教學 页面存档备份 存于互联网档案馆 取自 https zh wikipedia org w index php title Rust amp oldid 75672563, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。