Jump to content

Rust (programming language): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Conferences: fmt and linkify
Added plume to the list of rust projects
Line 168: Line 168:
* Pijul – a [[distributed version control]] system inspired by [[Darcs]]<ref>{{cite web|title=Pijul|url=https://pijul.org/|website=pijul.org|access-date=8 July 2017}}</ref>
* Pijul – a [[distributed version control]] system inspired by [[Darcs]]<ref>{{cite web|title=Pijul|url=https://pijul.org/|website=pijul.org|access-date=8 July 2017}}</ref>
* Piston – a [[game engine]]<ref>{{cite web |title=Piston A modular game engine written in Rust |url=http://www.piston.rs/ |work=Piston.rs |access-date=2017-08-01}}</ref>
* Piston – a [[game engine]]<ref>{{cite web |title=Piston A modular game engine written in Rust |url=http://www.piston.rs/ |work=Piston.rs |access-date=2017-08-01}}</ref>
*[https://joinplu.me Plume] – a [[Federation (information technology)|federated]] blogging platform
* rav1e - an [[AV1]] video encoder currently in active development.<ref>{{cite web |title=xiph/rav1e: The fastest and safest AV1 encoder. |url=https://github.com/xiph/rav1e|access-date=2018-10-18}}</ref>
* rav1e - an [[AV1]] video encoder currently in active development.<ref>{{cite web |title=xiph/rav1e: The fastest and safest AV1 encoder. |url=https://github.com/xiph/rav1e|access-date=2018-10-18}}</ref>
* REmacs – a port of [[Emacs]] to Rust<ref>{{cite web|last1=Larabel|first1=Michael|url=https://www.phoronix.com/scan.php?page=news_item&px=Remacs-Rust-Emacs |title=Remacs:Re-Implementing Emacs In Rust|website=phoronix.com|date=2017-01-11|access-date=2017-01-19}}</ref>
* REmacs – a port of [[Emacs]] to Rust<ref>{{cite web|last1=Larabel|first1=Michael|url=https://www.phoronix.com/scan.php?page=news_item&px=Remacs-Rust-Emacs |title=Remacs:Re-Implementing Emacs In Rust|website=phoronix.com|date=2017-01-11|access-date=2017-01-19}}</ref>

Revision as of 15:19, 16 December 2018

Rust
ParadigmMulti-paradigm: concurrent, functional, generic, imperative, structured
Designed byGraydon Hoare
DeveloperThe Rust Project
First appearedJuly 7, 2010; 14 years ago (2010-07-07)
Stable release
1.31.0[1] / December 6, 2018; 5 years ago (2018-12-06)
Typing disciplineInferred, linear, nominal, static, strong
Implementation languageRust
OSLinux, macOS, Windows, FreeBSD, OpenBSD,[2] Redox, Android, iOS (partial)[3]
LicenseMIT License or Apache License 2.0[4]
Filename extensions.rs, .rlib
Websitewww.rust-lang.org
Influenced by
Alef,[5] C#,[5] C++,[5] Cyclone,[5][6] Erlang,[5] Haskell,[5] Haxe,[5] Limbo,[5] Newsqueak,[5] OCaml,[5] Scheme,[5] Standard ML,[5] Swift[5][7]
Influenced
Crystal, Elm,[8] Idris,[9] Swift[10]

Rust is a systems programming language[11] with a focus on safety, especially safe concurrency,[12][13] supporting both functional and imperative paradigms. Rust is syntactically similar to C++,[14] but its designers intend it to provide better memory safety while still maintaining performance.

Rust was originally designed by Graydon Hoare at Mozilla Research, with contributions from Dave Herman, Brendan Eich, and many others.[15][16] Its designers have refined the language through the experiences of writing the Servo web browser layout engine[17] and the Rust compiler. The compiler is free and open-source software, dual-licensed under the MIT License and Apache License 2.0.

Rust won first place for "most loved programming language" in the Stack Overflow Developer Survey in 2016, 2017, and 2018.[18][19][20]

Design and features

A presentation on Rust by Emily Dunham from Mozilla's Rust team (Linux conference, Hobart, 2017).

Rust is intended to be a language for highly concurrent and highly safe systems,[21] and "programming in the large", that is, creating and maintaining boundaries that preserve large-system integrity.[22] This has led to a feature set with an emphasis on safety, control of memory layout, and concurrency.

Performance of idiomatic Rust

Performance of idiomatic Rust is comparable to the performance of idiomatic C++.[23][24]

Syntax

The concrete syntax of Rust is similar to C and C++, with blocks of code delimited by curly brackets, and control flow keywords such as if, else, while, and for. Not all C or C++ keywords are implemented, however, and some Rust functions (such as the use of the keyword match for pattern matching) will be less familiar to those versed in these languages. Despite the superficial resemblance to C and C++, the syntax of Rust in a deeper sense is closer to that of the ML family of languages as well as the Haskell language. Nearly every part of a function body is an expression,[25] even control flow operators. For example, the ordinary if expression also takes the place of C's ternary conditional. A function need not end with a return expression: in this case the last expression in the function creates the return value.

Memory safety

The system is designed to be memory safe, and it does not permit null pointers, dangling pointers, or data races in safe code.[26][27][28][29] Data values can only be initialized through a fixed set of forms, all of which require their inputs to be already initialized.[30] To replicate the function in other languages of pointers being either valid or NULL, such as in linked list or binary tree data structures, the Rust core library provides an option type, which can be used to test if a pointer has Some value or None.[27] Rust also introduces added syntax to manage lifetimes, and the compiler reasons about these through its borrow checker.

Memory management

Rust does not use an automated garbage collection system like those used by Go, Java, or the .NET Framework. Instead, memory and other resources are managed through resource acquisition is initialization (RAII), with optional reference counting. Rust provides deterministic management of resources, with very low overhead.[citation needed] Rust also favors stack allocation of values and does not perform implicit boxing.

There is also a concept of references (using the & symbol), which do not involve run-time reference counting. The safety of using such pointers is verified at compile time by the borrow checker, preventing dangling pointers and other forms of undefined behavior.

Ownership

Rust has an ownership system where all values have a unique owner where the scope of the value is the same as the scope of the owner.[31] Values can be passed by immutable reference using &T, by mutable reference using &mut T or by value using T. At all times, there can either be multiple immutable references or one mutable reference. The Rust compiler enforces these rules at compile time and also checks that all references are valid.

Types and polymorphism

The type system supports a mechanism similar to type classes, called "traits", inspired directly by the Haskell language. This is a facility for ad hoc polymorphism, achieved by adding constraints to type variable declarations. Other features from Haskell, such as higher-kinded polymorphism, are not yet supported.

Rust features type inference, for variables declared with the let keyword. Such variables do not require a value to be initially assigned to determine their type. A compile time error results if any branch of code fails to assign a value to the variable.[32] Variables assigned multiple times must be marked with the mut keyword.

Functions can be given generic parameters, which usually require the generic type to implement a certain trait or traits. Within such a function, the generic value can only be used through those traits. This means that a generic function can be type-checked as soon as it is defined. This is in contrast to C++ templates, which are fundamentally duck typed and cannot be checked until instantiated with concrete types. C++ concepts address the same issue and are expected to be part of C++20 (2020).

However, the implementation of Rust generics is similar to the typical implementation of C++ templates: a separate copy of the code is generated for each instantiation. This is called monomorphization and contrasts with the type erasure scheme typically used in Java and Haskell. The benefit of monomorphization is optimized code for each specific use case; the drawback is increased compile time and size of the resulting binaries.

The object system within Rust is based around implementations, traits and structured types. Implementations fulfill a role similar to that of classes within other languages, and are defined with the impl keyword. Inheritance and polymorphism are provided by traits; they allow methods to be defined and mixed in to implementations. Structured types are used to define fields. Implementations and traits cannot define fields themselves, and only traits can provide inheritance. Among other benefits, this prevents the diamond problem of multiple inheritance, as in C++. In other words, Rust supports interface inheritance, but replaces implementation inheritance with composition; see composition over inheritance.

History

The language grew out of a personal project started in 2006 by Mozilla employee Graydon Hoare,[13] who stated that the project was possibly named after the rust family of fungi.[33] Mozilla began sponsoring the project in 2009[13] and announced it in 2010.[34][35] The same year, work shifted from the initial compiler (written in OCaml) to the self-hosting compiler written in Rust.[36] Named rustc, it successfully compiled itself in 2011.[37] rustc uses LLVM as its back end.

The first numbered pre-alpha release of the Rust compiler occurred in January 2012.[38] Rust 1.0, the first stable release, was released on May 15, 2015.[39][40] Following 1.0, stable point releases are delivered every six weeks, while features are developed in nightly Rust and then tested with alpha and beta releases that last six weeks.[41]

In addition to conventional static typing, before version 0.4, Rust also supported typestates. The typestate system modeled assertions before and after program statements, through use of a special check statement. Discrepancies could be discovered at compile time, rather than when a program was running, as might be the case with assertions in C or C++ code. The typestate concept was not unique to Rust, as it was first introduced in the language NIL.[42] Typestates were removed because in practice they found little use, though the same function can still be achieved with branding patterns.[43]

The style of the object system changed considerably within versions 0.2, 0.3 and 0.4 of Rust. Version 0.2 introduced classes for the first time, with version 0.3 adding several features including destructors and polymorphism through the use of interfaces. In Rust 0.4, traits were added as a means to provide inheritance; interfaces were unified with traits and removed as a separate feature. Classes were also removed, replaced by a combination of implementations and structured types.[citation needed]

Starting in Rust 0.9 and ending in Rust 0.11, Rust had two built-in pointer types, ~ and @, simplifying the core memory model. It reimplemented those pointer types in the standard library as Box and (the now removed) Gc.

In January 2014, the editor-in-chief of Dr Dobb's, Andrew Binstock, commented on Rust's chances to become a competitor to C++, as well as to the other upcoming languages D, Go and Nim (then Nimrod): according to Binstock, while Rust was "widely viewed as a remarkably elegant language", adoption slowed because it changed repeatedly between versions.[44]

Rust was the third most loved programming language in the 2015 Stack Overflow annual survey,[45] and took first place in 2016, 2017, and 2018.[46][47][48]

The language is referenced in The Book of Mozilla as "oxidised metal."

Code examples

Hello World

Here is a simple "Hello, World!" program written in Rust. The println! macro prints the message to standard output.

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

Factorial function

Recursive

fn factorial(i: u64) -> u64 {
    match i {
        0 => 1,
        n => n * factorial(n-1)
    }
}

Iterative

fn factorial(i: u64) -> u64 {
    let mut acc = 1;
    for num in 2..=i {
        acc *= num;
    }
    acc
}

Using Iterators

fn factorial(i: u64) -> u64 {
    (1..=i).product()
}

Projects using Rust

The Rust compiler is written in Rust.[36]

Other projects developed in Rust include:

Web browser oriented:

Build tool oriented:

Operating system oriented:

Other projects:

Conferences

  • RustFest – Europe's @rustlang conference[86]

See also

References

  1. ^ "Announcing Rust 1.31 and Rust 2018". The Rust Programming Language Blog. 6 December 2018. Retrieved 6 December 2018.
  2. ^ "OpenBSD ports". Retrieved 2018-04-03.
  3. ^ "Rust on iOS". GitHub. 2015-01-09. Archived from the original on 2014-07-23. Retrieved 2017-06-22. {{cite web}}: Unknown parameter |dead-url= ignored (|url-status= suggested) (help)
  4. ^ a b c d e f g h i j k l m "The Rust Reference: Appendix: Influences". Retrieved November 11, 2018. 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# [...]
  5. ^ "Note Research: Type System". 2015-02-01. Retrieved 2015-03-25. 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". Retrieved December 4, 2014.
  7. ^ "Command Optimizations?". 2014-06-26. Retrieved 2014-12-10. 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.
  8. ^ ""Idris - Uniqueness Types"". Retrieved 2018-11-20.
  9. ^ http://nondot.org/sabre/
  10. ^ "Rust is a systems programming language". Rust-lang.org. Retrieved 2017-07-17.
  11. ^ Rust is mostly safety, Graydon Hoare, 2016-12-28.
  12. ^ a b c "FAQ – The Rust Project". Rust-lang.org. Retrieved 2 March 2016.
  13. ^ ""Rust vs. C++ Comparison"". Retrieved 20 November 2018. Rust is syntactically similar to C++, but it provides increased speed and better memory safety.
  14. ^ Noel (2010-07-08). "The Rust Language". Lambda the Ultimate. Retrieved 2010-10-30.
  15. ^ "Contributors to rust-lang/rust". Github.com. Retrieved 2018-10-12.
  16. ^ Bright, Peter (2013-04-03). "Samsung teams up with Mozilla to build browser engine for multicore machines". Arstechnica.com. Retrieved 2013-04-04.
  17. ^ "Stack Overflow Developer Survey 2016 Results". Stack Overflow. Retrieved 2017-03-22.
  18. ^ "Stack Overflow Developer Survey 2017". Stack Overflow. Retrieved 2017-03-22.
  19. ^ "Stack Overflow Developer Survey 2018". Stack Overflow. Retrieved 2018-03-13.
  20. ^ Avram, Abel (2012-08-03). "Interview on Rust, a Systems Programming Language Developed by Mozilla". InfoQ. Retrieved 2013-08-17. GH: A lot of obvious good ideas, known and loved in other languages, haven't made it into widely used systems languages ... There were a lot of good competitors in the late 1970s and early 1980s in that space, and I wanted to revive some of their ideas and give them another go, on the theory that circumstances have changed: the internet is highly concurrent and highly security-conscious, so the design-tradeoffs that always favor C and C++ (for example) have been shifting.
  21. ^ Debian package description: rustc
  22. ^ Walton, Patrick (2010-12-05). "C++ Design Goals in the Context of Rust". Retrieved 2011-01-21. It's impossible to be 'as fast as C' in all cases while remaining safe ... C++ allows all sorts of low-level tricks, mostly involving circumventing the type system, that offer practically unlimited avenues for optimization. In practice, though, C++ programmers restrict themselves to a few tools for the vast majority of the code they write, including stack-allocated variables owned by one function and passed by alias, uniquely owned objects (often used with auto_ptr or the C++0x unique_ptr), and reference counting via shared_ptr or COM. One of the goals of Rust's type system is to support these patterns exactly as C++ does, but to enforce their safe usage. In this way, the goal is to be competitive with the vast majority of idiomatic C++ in performance, while remaining memory-safe ...
  23. ^ "How Fast Is Rust?". The Rust Programming Language FAQ. Retrieved 3 August 2016.
  24. ^ "rust/src/grammar/parser-lalr.y". 2017-05-23. Retrieved 2017-05-23.
  25. ^ Rosenblatt, Seth (2013-04-03). "Samsung joins Mozilla's quest for Rust". Retrieved 2013-04-05. [Brendan Eich] noted that every year browsers fall victim to hacking in the annual Pwn2Own contest at the CanSecWest conference. "There's no free memory reads" in Rust, he said, but there are in C++. Those problems "lead to a lot of browser vulnerabilities" and would be solved by Rust, which is a self-compiling language.
  26. ^ a b Brown, Neil (2013-04-17). "A taste of Rust". Retrieved 2013-04-25. ... Other more complex data structures could clearly be implemented to allow greater levels of sharing, while making sure the interface is composed only of owned and managed references, and thus is safe from unplanned concurrent access and from dangling pointer errors.
  27. ^ "`unsafe` – The Rust Programming Language".
  28. ^ "Data Races and Race Conditions".
  29. ^ "The Rust Language FAQ". static.rust-lang.org. 2015. Archived from the original on 2015-04-20. Retrieved 2017-04-24. {{cite web}}: Unknown parameter |dead-url= ignored (|url-status= suggested) (help)
  30. ^ "What is Ownership? – What is Ownership? – The Rust Programming Language". doc.rust-lang.org. Retrieved 2018-02-20.
  31. ^ Walton, Patrick (2010-10-01). "Rust Features I: Type Inference". Retrieved 2011-01-21.
  32. ^ Hoare, Graydon (2014-06-07). "Internet archaeology: the definitive, end-all source for why Rust is named "Rust"". Reddit.com. Retrieved 2016-11-03.
  33. ^ "Future Tense". 2011-04-29. Retrieved 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.
  34. ^ Hoare, Graydon (7 July 2010). Project Servo (pdf). Mozilla Annual Summit 2010. Whistler, Canada. Retrieved 22 February 2017.
  35. ^ a b Hoare, Graydon (2010-10-02). "Rust Progress". Archived from the original on 2014-08-15. Retrieved 2010-10-30.
  36. ^ Hoare, Graydon (2011-04-20). "[rust-dev] stage1/rustc builds". Retrieved 2011-04-20. After that last change fixing the logging scope context bug, looks like stage1/rustc builds. Just shy of midnight :)
  37. ^ catamorphism (2012-01-20). "Mozilla and the Rust community release Rust 0.1 (a strongly-typed systems programming language with a focus on memory safety and concurrency)". Retrieved 2012-02-06.
  38. ^ "Version History". Retrieved 2017-01-01.
  39. ^ The Rust Core Team (May 15, 2015). "Announcing Rust 1.0". Retrieved 2015-12-11.
  40. ^ "Scheduling the Trains". Retrieved 2017-01-01.
  41. ^ Strom, Robert E.; Yemini, Shaula (1986). "Typestate: A Programming Language Concept for Enhancing Software Reliability" (PDF). IEEE Transactions on Software Engineering. ISSN 0098-5589. Retrieved 2010-11-14. {{cite journal}}: Cite journal requires |journal= (help)
  42. ^ Walton, Patrick (2012-12-26). "Typestate Is Dead, Long Live Typestate!". Pcwalton.github.com. Retrieved 2016-11-03.
  43. ^ Binstock, Andrew. "The Rise And Fall of Languages in 2013". Dr Dobb's.
  44. ^ "Stack Overflow Developer Survey 2015". Stackoverflow.com. Retrieved 2016-11-03.
  45. ^ "Stack Overflow Developer Survey 2016 Results". Stack Overflow. Retrieved 2017-03-22.
  46. ^ "Stack Overflow Developer Survey 2017". Stack Overflow. Retrieved 2017-03-22.
  47. ^ "Stack Overflow Developer Survey 2018". Stack Overflow. Retrieved 2018-03-13.
  48. ^ Herman, Dave (2016-07-12). "Shipping Rust in Firefox * Mozilla Hacks – the Web developer blog". Hacks.mozilla.org. Retrieved 2016-11-03.
  49. ^ Yegulalp, Serdar (3 April 2015). "Mozilla's Rust-based Servo browser engine inches forward". InfoWorld. Retrieved 2016-03-15.
  50. ^ Lardinois, Frederic (3 April 2015). "Mozilla And Samsung Team Up To Develop Servo, Mozilla's Next-Gen Browser Engine For Multicore Processors". TechCrunch.
  51. ^ Bryant, David. "A Quantum Leap for the Web". Medium. Retrieved 27 October 2016.
  52. ^ "Automate any app, anywhere with Habitat". Chef Software. Retrieved 2017-08-01.
  53. ^ Metz, Cade (2016-03-14). "The Epic Story of Dropbox's Exodus From the Amazon Cloud Empire". Wired.com. Retrieved 2016-11-03.
  54. ^ Yegulalp, Serdar. "Rust's Redox OS could show Linux a few new tricks". infoworld. Retrieved 21 March 2016.
  55. ^ Sei, Mark (30 August 2017). "Red Hat deprecates BTRFS, is Stratis the new ZFS-like hope?". Marksei, Weekly sysadmin pills.
  56. ^ "Tock Embedded Operating System". Tock Embedded Operating System. Retrieved 2017-11-13.
  57. ^ "Building a Container Runtime in Rust". 29 June 2017. Retrieved 8 July 2017. Why Rust? (…) Rust sits at a perfect intersection of [C and Go]: it has memory safety and higher-level primitives, but sacrifices no low level control over threading, and thus can handle namespaces properly.
  58. ^ "jwilm/alacritty". GitHub. Retrieved 2018-04-06.
  59. ^ "Data-oriented game engine written in Rust". Amethyst.rs. Retrieved 2017-10-07.
  60. ^ "CITA". Github. Retrieved 2017-09-09.
  61. ^ "Conduit".
  62. ^ "Grin".
  63. ^ "Gtk-rs organization". gtk-rs. Retrieved 2016-01-01.
  64. ^ https://holochain.org/
  65. ^ "Building the Metaverse". Lucidscape.com. Retrieved 2017-08-01.
  66. ^ "The IoT Edge OSS project". Github. Retrieved 2018-09-26.
  67. ^ "OneSignal now sends iOS push notifications 100x faster". OneSignal. 2016-03-21. Retrieved 2017-01-06.
  68. ^ Balbaert, Ivo. Rust Essentials. Packt Publishing. p. 6. ISBN 1785285769. Retrieved 21 March 2016.
  69. ^ Frank, Denis. "Using HyperLogLog to Detect Malware Faster Than Ever". OpenDNS Security Labs. Retrieved 19 March 2016.
  70. ^ Denis, Frank. "ZeroMQ: Helping us Block Malicious Domains in Real Time". OpenDNS Security Labs. Retrieved 19 March 2016.
  71. ^ https://p3ki.com/
  72. ^ https://github.com/paritytech
  73. ^ "Pijul". pijul.org. Retrieved 8 July 2017.
  74. ^ "Piston A modular game engine written in Rust". Piston.rs. Retrieved 2017-08-01.
  75. ^ "xiph/rav1e: The fastest and safest AV1 encoder". Retrieved 2018-10-18.
  76. ^ Larabel, Michael (2017-01-11). "Remacs:Re-Implementing Emacs In Rust". phoronix.com. Retrieved 2017-01-19.
  77. ^ "Rustation". Retrieved 8 July 2017.
  78. ^ "The SAFE Network – Building the new decentralized Internet". Retrieved 2018-10-07.
  79. ^ Hahn, Sebastian (2017-03-31). "[tor-dev] Tor in a safer language: Network team update from Amsterdam". Retrieved 2017-04-01.
  80. ^ asn (2017-07-05). "The Wilmington Watch: A Tor Network Team Hackfest". Tor Blog. Retrieved 2018-01-03.
  81. ^ "Hey, this is kyren from Chucklefish, we make and publish cool video games. One of our two next projects is currently being written in rust, and I'd like to talk to you about it! • r/rust". reddit. Retrieved 2017-10-27.
  82. ^ Levien, Raph (2018-01-23). "Xi: an editor for the next 20 years". Recurse Center. Retrieved 2018-08-08.
  83. ^ "xi-editor". Google Open Source. Retrieved 2018-08-08.
  84. ^ https://rust-belt-rust.com/past
  85. ^ https://blog.rustfest.eu/past_events/