Rust is a systems programming language sponsored by Mozilla Research, which describes it as a "safe, concurrent, practical language," supporting functional and imperative-procedural paradigms. Rust is syntactically similar to C++, but its designers intend it to provide better memory safety while still maintaining performance.
Rust is an open source programming language. Its designers have refined the language through the experiences of writing the Servo web browser layout engine and the Rust compiler. A large proportion of current commits to the project are from community members.
Rust won first place for "most loved programming language" in the Stack Overflow Developer Survey in 2016, 2017 and 2018. The language is referenced in The Book of Mozilla as "oxidised metal."
Video Rust (programming language)
Design and features
Rust is intended to be a language for highly concurrent and highly safe systems, and "programming in the large", that is, creating and maintaining boundaries that preserve large-system integrity. This has led to a feature set with an emphasis on safety, control of memory layout, and concurrency. Performance of idiomatic Rust is comparable to the performance of idiomatic C++.
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 functionality (such as the use of the keyword match
for pattern matching) will be less familiar to programmers coming from 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. Nearly every part of a function body is an expression, 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. Data values can only be initialized through a fixed set of forms, all of which require their inputs to be already initialized. To replicate the functionality 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
. Rust also introduces additional 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 .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. 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. 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. 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.
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.
Maps Rust (programming language)
History
The language grew out of a personal project started in 2006 by Mozilla employee Graydon Hoare, who stated that the project was possibly named after the rust family of fungi. Mozilla began sponsoring the project in 2009 and announced it in 2010. The same year, work shifted from the initial compiler (written in OCaml) to the self-hosting compiler written in Rust. Known as rustc, it successfully compiled itself in 2011. rustc uses LLVM as its back end.
The first numbered pre-alpha release of the Rust compiler occurred in January 2012. Rust 1.0, the first stable release, was released on May 15, 2015. 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.
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. Typestates were removed because in practice they found little use, though the same functionality can still be achieved with branding patterns.
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 a number of 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, 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 repeatedly changed between versions.
Rust was the third most loved programming language in the 2015 Stack Overflow annual survey, and took first place in 2016, 2017, and 2018.
Code examples
Hello World
Here is a simple "Hello, World!" program written in Rust. The println!
macro prints the message to standard output and exits.
Factorial function
Recursive
Iterative
Projects using Rust
The Rust compiler itself is written in Rust.
Other projects developed in Rust include:
Web browser oriented:
- Firefox
- Servo - Mozilla's new parallel web browser engine developed in collaboration with Samsung
- Quantum - a project, composed of several sub-projects, to improve the Gecko web browser engine of Firefox, developed by Mozilla
Build tool oriented:
- Cargo - Rust's build automation system
- Habitat - a build and deployment tool from Chef Software
Operating system oriented:
- Magic Pocket - Dropbox's file system that powers their Diskotech petabyte storage machines
- Redox - a microkernel operating system
- Stratis - a file system planned for Fedora 28
- Tock - an embedded operating system
- Railcar - a container runtime by Oracle
Other projects:
- Alacritty - a cross-platform, GPU-accelerated terminal emulator.
- Exonum - an extensible open-source framework for building secure permissioned blockchain applications
- CITA - a fast and scalable permissioned blockchain for production
- OpenDNS - used in two of its components
- Piston - a game engine
- Amethyst - a data-oriented, data-driven game engine
- OnePush - a notification delivery system developed by OneSignal
- REmacs - a port of Emacs to Rust
- MAIDsafe - a P2P Internet project currently being developed by a team in Troon, Scotland
- Lucidscape Mesh - a distributed real-time simulation engine for virtual reality
- Tor - an anonymity network - is experimenting with porting to Rust for its security features. (Tor is currently written in C.)
- Pijul - a distributed version control system inspired by Darcs
- Rustation - a PlayStation emulator
- Parity - a Ethereum web browser and wallet management application
- WitchBrook - a video game by Chucklefish
- TiKV - a distributed storage engine for TiDB, a distributed hybrid transactional/analytical processing (HTAP) database
- Xi - a free modern text editor from Google Open Source
- Grin - Grin is an open source distributed ledger cryptocurrency software project that implements a MimbleWimble Blockchain
- Conduit - service mesh for Kubernetes from Buoyant Inc, the makers of Linkerd
See also
- Comparison of programming languages
References
External links
- Official website
- The Rust-dev Archives - electronic mailing list
- rust on GitHub - primary source code repository and bug tracker
- rust-rosetta on GitHub - implementations of common algorithms and solutions
- Rust by Example - web book
- Rust SubReddit - active dev community language development and support
- Rust compared to other programming languages - interactive comparison
Source of article : Wikipedia