Rust
外观

![]() | |
编程范型 | 編譯語言,並行計算, 函數程式語言,指令式編程, 物件導向程式設計,結構化編程 |
---|---|
設計者 | Graydon Hoare |
實作者 | Mozilla |
发行时间 | 2010年 |
当前版本 |
|
型態系統 | 靜態類型,強型別,類型推論,結構類型 |
操作系统 | Linux Mac OS X Windows FreeBSD |
許可證 | MIT |
文件扩展名 | .rs |
網站 | rust-lang.org |
受影响于 | |
Alef C♯ C++ Camlp4 Common Lisp Erlang Haskell Hermes Limbo Napier Napier88 Newsqueak NIL Ruby Sather Standard ML |
Rust是一个由Mozilla[3]主导开发的实验性跨平台编译型编程语言。它的设计准则为“安全,并发,实用”,[4][5]支持函数式,并发式,程序式以及面向对象的编程风格。
历史
2006年,Rust作为Graydon Hoare的个人项目首次出现。
2009年,Graydon Hoare成为Mozilla雇员[6]。
2010年,Rust首次作为Mozilla官方项目出现[7]。同年,Rust开始从初始编译(由OCaml写成)转变为自编译[8]。
2011年,Rust成功的完成了移植[9]。Rust的自编译器采用LLVM作为其编译后端。
2012年1月20日,第一个有版本号的预览版Rust编译器发布[10]。当前最新版本为0.7,于2013年7月发布[2]。
2013年4月4日,Mozilla基金會宣布將與三星集團合作開發瀏覽器排版引擎Servo将以Rust來實作[11]。
代码示例
下面的代码在Rust 0.7中测试通过,在将来的版本中语法和语义可能会发生变化。
Hello World
fn main() {
println("hello, world");
}
阶乘
/* The branches in this function exhibit Rust's optional implicit return values,
which can be utilized where a more "functional" style is preferred.
Unlike C++ and related languages, Rust's `if` construct is an expression
rather than a statement, and thus has a return value of its own. */
fn fac_recur(n: int) -> int {
if n <= 1 { 1 }
else { n * fac_recur(n-1) }
}
fn fac_iter(n: int) -> int {
// Variables must be declared with the `mut` keyword in order to be mutable.
let mut i = 1,
result = 1;
while i <= n {
result *= i;
i += 1;
}
return result; // An explicit return, in contrast to the above.
}
并发
一个简单的Rust并发示例:
use task::spawn;
/* This function creates ten "tasks" that all execute concurrently.
Run it to observe the irregular linebreaks that result as each task races
on stdout, due to the fact that `io::println` may yield to the task
scheduler between printing its argument and printing a newline. */
fn main() {
// `times` is a method on numeric types whose only argument is a closure.
for 10.times {
do spawn {
// `each` is a method that accepts a closure with one argument.
for [1, 2, 3].each |item| {
// `fmt!` is a macro that statically checks its format string.
io::println(fmt!("%d", *item));
}
}
}
}
开发过程
在早期的开发版本中,Rust禁止使用非ASCII码字符作为变量名,主要理由在于大多数非英语使用者仍会使用ASCII字符作为变量名,然而,这个限制遭到了许多批评[12]。在2011年2月的一次更新中,Rust去除了这个限制[13]。
参见
參考資料
- ^ Announcing Rust 1.88.0. 2025年6月26日 [2025年6月26日] (英語).
- ^ 2.0 2.1 Rust Release Notes
- ^ The Rust Language. Lambda the Ultimate. 2010-07-08 [2010-10-30]. Authors list列表中的
|first1=
缺少|last1=
(帮助) - ^ The Rust Programming Language. [2012-10-21].
- ^ Doc language FAQ. [2012-10-21].
- ^ Project FAQ. 2010-09-14 [2012-01-11].
- ^ Future Tense. 2011-04-29 [2012-02-06].
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.
- ^ Hoare, Graydon. Rust Progress. 2010-10-02 [2010-10-30].
- ^ Hoare, Graydon. [rust-dev] stage1/rustc builds. 2011-04-20 [2011-04-20].
After that last change fixing the logging scope context bug, looks like stage1/rustc builds. Just shy of midnight :)
- ^ 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].
- ^ http://arstechnica.com/information-technology/2013/04/samsung-teams-up-with-mozilla-to-build-browser-engine-for-multicore-machines/
- ^ Jelliffe, Rick. Vale Java? Scala Vala palava. 2010-11-08 [2012-03-29].
… It is just plain ignorant to say that non-English programmers always write with ASCII. (Just as it would be ignorant to say that they never do.) It is that kind of rather blithe dismissal that foreign cultures and languages need to be supported that creates extra unnecessary barriers. That argument ran out of legs in the early 1990s: all platforms have well -established Unicode libraries with serviceable properties for this…
- ^ Commit dabccadd3202513ab0bcb424e2c62c90ab23062d. 2011-02-26 [2012-01-11].