Rust (programming language)

Rust is a multi-paradigm programming language designed for performance and safety, especially safe concurrency.[15][16] Rust is syntactically similar to C++,[17] but can guarantee memory safety by using a borrow checker to validate references.[18] Rust achieves memory safety without garbage collection, and reference counting is optional.[19][20]

Rust
Official Rust logo
ParadigmsMulti-paradigm: concurrent, functional, generic, imperative, structured
Designed byGraydon Hoare
First appearedJuly 7, 2010 (2010-07-07)
Stable release
1.49.0[1]  / 31 December 2020 (31 December 2020)
Typing disciplineAffine, inferred, nominal, static, strong
Implementation languageRust
PlatformARM, IA-32, x86-64, MIPS, PowerPC, SPARC, RISC-V[2][3]
OSLinux, macOS, Windows, FreeBSD, OpenBSD,[4] Redox, Android, iOS[5]
LicenseMIT or Apache 2.0[6]
Filename extensions.rs, .rlib (metadata file)
Websitewww.rust-lang.org
Influenced by
Alef,[7] C#,[7] C++,[7] Cyclone,[7][8] Erlang,[7] Haskell,[7] Limbo,[7] Newsqueak,[7] OCaml,[7] Ruby,[7] Scheme,[7] Standard ML,[7] Swift[7][9]
Influenced
Crystal, Elm,[10] Idris,[11] Spark,[12] Swift,[13] Project Verona,[14] Zig

Rust was originally designed by Graydon Hoare at Mozilla Research, with contributions from Dave Herman, Brendan Eich, and others.[21][22] The designers refined the language while writing the Servo layout or browser engine,[23] and the Rust compiler. It has gained increasing use in industry, and Microsoft has been experimenting with the language for secure and safety-critical software components.[24][25]

Rust has been voted the "most loved programming language" in the Stack Overflow Developer Survey every year since 2016.[26]

History

Some Rust users refer to themselves as Rustaceans (a pun on "crustacean") and use Ferris as their unofficial mascot.[27]

The language grew out of a personal project begun in 2006 by Mozilla employee Graydon Hoare,[16] who stated that the project was possibly named after the rust family of fungi.[28] Mozilla began sponsoring the project in 2009[16] and announced it in 2010.[29][30] The same year, work shifted from the initial compiler (written in OCaml) to the LLVM-based self-hosting compiler written in Rust.[31] Named rustc, it successfully compiled itself in 2011.[32]

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

Along with 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.[37] Typestates were removed because in practice they were little used,[38] though the same function can be achieved by leveraging Rust's move semantics.[39]

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, and version 0.3 added 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.

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, before the first stable release, Rust 1.0, the editor-in-chief of Dr Dobb's, Andrew Binstock, commented on Rust's chances of becoming a competitor to C++ and to the other up-and-coming languages D, Go, and Nim (then Nimrod). According to Binstock, while Rust was "widely viewed as a remarkably elegant language", adoption slowed because it repeatedly changed between versions.[40]

Rust has a foreign function interface (FFI) that can be called from e.g. C language, and can call C, while calling C++ has historically been problematic (from any language). Rust has a library, CXX, to allow calling to or from C++, and "CXX has zero or negligible overhead".[41]

In August 2020, Mozilla laid off 250 of its 1,000 employees worldwide as part of a corporate restructuring caused by the long-term impact of the COVID-19 pandemic.[42][43] Among those laid off were most of the Rust team,[44] while the Servo team was completely disbanded.[45] The event raised concerns about the future of Rust.[46]

In the following week, the Rust Core Team acknowledged the severe impact of the layoffs and announced that plans for a Rust foundation were underway. The first goal of the foundation would be taking ownership of all trademarks and domain names, and also take financial responsibility for their costs.[47]

On February 8, 2021 the formation of the Rust Foundation was officially announced by its five founding companies (AWS, Huawei, Google, Microsoft, and Mozilla).[48][49]

Syntax

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

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

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, although the specific syntax for defining functions is more similar to Pascal. 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 and the Haskell language. Nearly every part of a function body is an expression,[50] even control flow operators. For example, the ordinary if expression also takes the place of C's ternary conditional, an idiom used by ALGOL-60. As in Lisp, a function need not end with a return expression: in this case if the semicolon is omitted, the last expression in the function creates the return value, as seen in the following recursive implementation of the factorial function:

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

The following iterative implementation uses the ..= operator to create an inclusive range:

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

Features

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

Memory safety

Rust is designed to be memory safe, and it does not permit null pointers, dangling pointers, or data races in safe code.[53][54][55] Data values can be initialized only through a fixed set of forms, all of which require their inputs to be already initialized.[56] 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 whether a pointer has Some value or None.[54] Rust also introduces added syntax to manage lifetimes, and the compiler reasons about these through its borrow checker. Unsafe code which can subvert some of these restrictions may be written using the language's unsafe keyword.[18]

Memory management

Rust does not use an automated garbage collection system. Instead, memory and other resources are managed through the resource acquisition is initialization (RAII) convention,[57] with optional reference counting. Rust provides deterministic management of resources, with very low overhead. Rust also favors stack allocation of values and does not perform implicit boxing.

There is the concept of references (using the & symbol), which does 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. Additionally, Rust's type system separates shared, immutable pointers of the form &T from unique, mutable pointers of the form &mut T. However, a mutable pointer can be coerced to an immutable pointer, but not vice versa.

Ownership

Rust has an ownership system where all values have a unique owner, and the scope of the value is the same as the scope of the owner.[58][59] 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 (an implicit readers-writer lock). 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 keyword let. 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 leaves the variable without an assignment.[60] Variables assigned multiple times must be marked with the keyword mut.

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 part of C++20, though they still don't allow the C++ compiler to typecheck a template without concrete instantiation.

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. Type erasure is also available in Rust by using the keyword dyn. 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 keyword impl. 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.

Performance

Performance of idiomatic Rust is comparable to the performance of idiomatic C++.[61][62]

Adoption

Rust was the third-most-loved programming language in the 2015 Stack Overflow annual survey[63] and took first place for 2016–2020.[64]

Web browser

A web browser and several related components are being written in Rust. Firefox[65], for example, has two projects written in Rust, including Servo, a parallel web-browser engine[66] developed by Mozilla in collaboration with Samsung[67] and Quantum, which is composed of several sub projects, for improving the Gecko web-browser engine, which is also deveoped by Mozilla.[68]

Operating systems

Operating systems and OS-level components written in Rust include:

Other

Development

Rust conferences include:


See also

References

  1. "Announcing Rust 1.49.0". 31 December 2020.
  2. "Rust Platform Support". Rust Forge. Retrieved 2019-05-19.
  3. "Frequently Asked Questions". Rust Embedded. Retrieved 2019-05-14.
  4. "OpenBSD ports". Retrieved 2018-04-03.
  5. "Building and Deploying a Rust library on iOS". 6 September 2017. Retrieved 11 January 2019.
  6. "The Rust Reference: Appendix: Influences". Retrieved November 11, 2018.
  7. "Note Research: Type System". 2015-02-01. Retrieved 2015-03-25.
  8. "RFC for 'if let' expression". Retrieved December 4, 2014.
  9. "Command Optimizations?". 2014-06-26. Retrieved 2014-12-10.
  10. "Idris – Uniqueness Types". Retrieved 2018-11-20.
  11. Jaloyan, Georges-Axel (19 October 2017). "Safe Pointers in SPARK 2014". arXiv:1710.07047. Bibcode:2017arXiv171007047J. Cite journal requires |journal= (help)
  12. Lattner, Chris. "Chris Lattner's Homepage". Nondot.org. Retrieved 2019-05-14.
  13. "Microsoft opens up Rust-inspired Project Verona programming language on GitHub". Retrieved 2020-01-17.
  14. Hoare, Graydon (2016-12-28). "Rust is mostly safety". Graydon2. Dreamwidth Studios. Retrieved 2019-05-13.
  15. "FAQ – The Rust Project". Rust-lang.org. Archived from the original on 2016-06-09. Retrieved 27 June 2019.
  16. "Rust vs. C++ Comparison". Retrieved 20 November 2018.
  17. "Unsafe Rust". Retrieved 2020-10-17.
  18. "Fearless Security: Memory Safety". Retrieved 4 November 2020.
  19. "Rc<T>, the Reference Counted Smart Pointer". Retrieved 4 November 2020.
  20. Noel (2010-07-08). "The Rust Language". Lambda the Ultimate. Retrieved 2010-10-30.
  21. "Contributors to rust-lang/rust". GitHub. Retrieved 2018-10-12.
  22. Bright, Peter (2013-04-03). "Samsung teams up with Mozilla to build browser engine for multicore machines". Ars Technica. Retrieved 2013-04-04.
  23. "Why Rust for safe systems programming". Retrieved 2019-07-22.
  24. "How Microsoft Is Adopting Rust". Retrieved 2020-08-07.
  25. https://insights.stackoverflow.com/survey/2020#most-loved-dreaded-and-wanted
  26. "Getting Started". rust-lang.org. Retrieved 11 October 2020.
  27. Hoare, Graydon (2014-06-07). "Internet archaeology: the definitive, end-all source for why Rust is named "Rust"". Reddit.com. Retrieved 2016-11-03.
  28. "Future Tense". 2011-04-29. Retrieved 2012-02-06.
  29. Hoare, Graydon (7 July 2010). Project Servo (PDF). Mozilla Annual Summit 2010. Whistler, Canada. Retrieved 22 February 2017.
  30. Hoare, Graydon (2010-10-02). "Rust Progress". Archived from the original on 2014-08-15. Retrieved 2010-10-30.
  31. Hoare, Graydon (2011-04-20). "[rust-dev] stage1/rustc builds". Retrieved 2011-04-20.
  32. 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.
  33. "Version History". Retrieved 2017-01-01.
  34. The Rust Core Team (May 15, 2015). "Announcing Rust 1.0". Retrieved 2015-12-11.
  35. "Scheduling the Trains". Retrieved 2017-01-01.
  36. Strom, Robert E.; Yemini, Shaula (1986). "Typestate: A Programming Language Concept for Enhancing Software Reliability" (PDF). IEEE Transactions on Software Engineering: 157–171. doi:10.1109/TSE.1986.6312929. ISSN 0098-5589. S2CID 15575346. Retrieved 2010-11-14.
  37. Walton, Patrick (2012-12-26). "Typestate Is Dead, Long Live Typestate!". GitHub. Retrieved 2016-11-03.
  38. Biffle, Cliff (2019-06-05). "The Typestate Pattern in Rust". Retrieved 2021-02-01.
  39. Binstock, Andrew. "The Rise And Fall of Languages in 2013". Dr Dobb's.
  40. "Safe Interoperability between Rust and C++ with CXX". InfoQ. 2020-12-06. Retrieved 2021-01-03.
  41. Cimpanu, Catalin (2020-08-11). "Mozilla lays off 250 employees while it refocuses on commercial products". ZDNet. Retrieved 2020-12-02.
  42. Cooper, Daniel (2020-08-11). "Mozilla lays off 250 employees due to the pandemic". Engadget. Retrieved 2020-12-02.
  43. @tschneidereit (2020-08-12). "Much of the team I used to manage was part of the Mozilla layoffs on Tuesday. That team was Mozilla's Rust team, and Mozilla's Wasmtime team. I thought I'd know how to talk about it by now, but I don't. It's heartbreaking, incomprehensible, and staggering in its impact" (Tweet). Retrieved 2020-12-02 via Twitter.
  44. @asajeffrey (2020-08-11). "Mozilla is closing down the team I'm on, so I am one of the many folks now wondering what the next gig will be. It's been a wild ride!" (Tweet). Retrieved 2020-12-02 via Twitter.
  45. Kolakowski, Nick (2020-08-27). "Is Rust in Trouble After Big Mozilla Layoffs?". Dice. Retrieved 2020-12-02.
  46. "Laying the foundation for Rust's future". Rust Blog. 2020-08-18. Retrieved 2020-12-02.
  47. "Rust Foundation". foundation.rust-lang.org. 2021-02-08. Retrieved 2021-02-09.
  48. "Mozilla Welcomes the Rust Foundation". Mozilla Blog. 2021-02-09. Retrieved 2021-02-09.
  49. "rust/src/grammar/parser-lalr.y". 2017-05-23. Retrieved 2017-05-23.
  50. Avram, Abel (2012-08-03). "Interview on Rust, a Systems Programming Language Developed by Mozilla". InfoQ. Retrieved 2013-08-17.
  51. "Debian package description: rustc".
  52. Rosenblatt, Seth (2013-04-03). "Samsung joins Mozilla's quest for Rust". Retrieved 2013-04-05.
  53. Brown, Neil (2013-04-17). "A taste of Rust". Retrieved 2013-04-25.
  54. "Data Races and Race Conditions".
  55. "The Rust Language FAQ". static.rust-lang.org. 2015. Archived from the original on 2015-04-20. Retrieved 2017-04-24.
  56. "RAII - Rust By Example". doc.rust-lang.org. Retrieved 2020-11-22.
  57. Klabnik, Steve; Nichols, Carol (June 2018). "Chapter 4: Understanding Ownership". The Rust Programming Language. San Francisco, California: No Starch Press. p. 44. ISBN 978-1-593-27828-1. Retrieved 2019-05-14.
  58. "The Rust Programming Language: What is Ownership". Rust-lang.org. Retrieved 2019-05-14.
  59. Walton, Patrick (2010-10-01). "Rust Features I: Type Inference". Retrieved 2011-01-21.
  60. Walton, Patrick (2010-12-05). "C++ Design Goals in the Context of Rust". Retrieved 2011-01-21.
  61. "How Fast Is Rust?". The Rust Programming Language FAQ. Retrieved 11 April 2019.
  62. "Stack Overflow Developer Survey 2015". Stackoverflow.com. Retrieved 2016-11-03.
  63. https://insights.stackoverflow.com/survey/2019
  64. Herman, Dave (2016-07-12). "Shipping Rust in Firefox * Mozilla Hacks: the Web developer blog". Hacks.mozilla.org. Retrieved 2016-11-03.
  65. Yegulalp, Serdar (2015-04-03). "Mozilla's Rust-based Servo browser engine inches forward". InfoWorld. Retrieved 2016-03-15.
  66. Lardinois, Frederic (2015-04-03). "Mozilla And Samsung Team Up To Develop Servo, Mozilla's Next-Gen Browser Engine For Multicore Processors". TechCrunch.
  67. Bryant, David (27 October 2016). "A Quantum Leap for the web". Medium. Retrieved 27 October 2016.
  68. Yegulalp, Serdar. "Rust's Redox OS could show Linux a few new tricks". infoworld. Retrieved 21 March 2016.
  69. Sei, Mark (10 October 2018). "Fedora 29 new features: Startis now officially in Fedora". Marksei, Weekly sysadmin pills. Retrieved 2019-05-13.
  70. "RHEL 8: Chapter 8. Managing layered local storage with Stratis". 10 October 2018.
  71. "Writing an OS in Rust".
  72. Nichols, Shaun (27 June 2018). "Microsoft's next trick? Kicking things out of the cloud to Azure IoT Edge". The Register. Retrieved 2019-09-27.
  73. Balbaert, Ivo (27 May 2015). Rust Essentials. Packt Publishing. p. 6. ISBN 978-1785285769. Retrieved 21 March 2016.
  74. Frank, Denis (5 December 2013). "Using HyperLogLog to Detect Malware Faster Than Ever". OpenDNS Security Labs. Retrieved 19 March 2016.
  75. Denis, Frank (4 October 2013). "ZeroMQ: Helping us Block Malicious Domains in Real Time". OpenDNS Security Labs. Retrieved 19 March 2016.
  76. Hahn, Sebastian (2017-03-31). "[tor-dev] Tor in a safer language: Network team update from Amsterdam". Retrieved 2017-04-01.
  77. asn (2017-07-05). "The Wilmington Watch: A Tor Network Team Hackfest". Tor Blog. Retrieved 2018-01-03.
  78. Garbutt, James (27 January 2019). "First thoughts on Deno, the JavaScript/TypeScript run-time". 43081j.com. Retrieved 2019-09-27.
  79. Howarth, Jesse (2020-02-04). "Why Discord is switching from Go to Rust". Retrieved 2020-04-14.
  80. terminusdb/terminusdb-store, TerminusDB, 2020-12-14, retrieved 2020-12-14
  81. "RustConf".
  82. Rust Belt Rust. Dayton, Ohio. 2019-10-18. Retrieved 2019-05-14.
  83. RustFest. Barcelona, Spain: asquera Event UG. 2019. Retrieved 2019-05-14.
  84. "Oxidize Global". Oxidize Berlin Conference. Retrieved 2021-02-01.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.