Rust (Language)
- Summary
-
Discussion
- For what use cases is Rust suitable?
- Which are main design aspects of Rust?
- Which are the basic programming constructs available in Rust?
- Is Rust an object-oriented or a functional language?
- Who has adopted Rust so far?
- What are some criticisms of Rust?
- What tools are available for Rust programming?
- Milestones
- Sample Code
- References
- Further Reading
- Article Stats
- Cite As
C and C++ are highly performant and offer low-level control, which is something systems programmers want. But they're not safe since it's easy to access wrong memory locations. Haskell is a lot safer but it doesn't offer low-level control. Go and Java are somewhere in between. In all these languages, trade-offs have been made. Rust is a programming language that's safe, performant, and offers low-level control.
Rust achieves this with strong static typing and compile-time checks. Many problems are caught at compile time rather than at runtime. The concept of ownership leads to memory-safe and thread-safe code. Rust itself is considered a multi-paradigm language with design elements taken from many other languages.
Discussion
-
For what use cases is Rust suitable? Systems programming is the main use case for Rust. Systems programming includes operating systems, file systems, and low-level drivers. We can also include any code that powers other applications, such as web browsers, web frameworks, and game engines. For systems programming, it's important to have safe memory management without runtime overhead. Untrusted code shouldn't be allowed to cause problems. We want low-level access so that we can optimize our code for time and space.
Rust is well-suited for embedded development. It can run on bare-metal systems without any RTOS. Pin and peripheral configurations can be enforced at compile time. We can choose to leave out the heap and allocate all memory statically. Concurrency is easy to achieve since Rust ensures that two threads will not accidentally share state. Interoperability and portability are other benefits.
Rust is capable of providing performance close to that of C++. Hence it's seen as a language for High-Performance Computing (HPC) and scientific computing.
The Rust community is also adopting Rust in a variety of areas including web development, distributed peer-to-peer computing, game development, GUI development, machine learning, quantum computing, audio processing, and more.
-
Which are main design aspects of Rust? Ownership is Rust's most unique feature. Each value can have only one owner. The value is dropped when the owner goes out of scope. Scope is implemented by another feature called lifetime. These bring memory safety to Rust programs. Heap memory is safely allocated and deallocated even without a garbage collector. Ownership can be borrowed or moved. For thread-safe code, Rust a thread can borrow ownership from another thread, thus preventing data races.
Most problems in Rust code are caught at compile time. As a result, runtime overheads are less. Rust abstractions are not expected to add overhead to equivalent low-level code. Rust calls this zero-cost abstractions. Rust doesn't support NULL pointers like in C/C++. Error handling is possible but there's no support for exceptions. Again, the philosophy is to catch most things at compile time.
Variables are immutable by default to promote safe coding. Another type-safe feature is that function parameters must include type annotations.
-
Which are the basic programming constructs available in Rust? Like most languages, Rust specifies language keywords, variables, data types, functions, function parameters, control flow, expressions, statements and comments. Scalar types include signed/unsigned integers, floating-point numbers, Booleans, and characters. Compound types include tuples and arrays, both of fixed length. Tuples can contain items of different types but array items must be of the same type. Data structures called collections are allocated on the heap. Vectors, strings and hash maps are examples.
Custom data types can be defined using
struct
andenum
. Instruct
definitions we can include associated behaviour using methods. Withenum
, we can use thematch
expression to implement powerful behaviours including theswitch-case
construct common in other languages. Generics and traits enable reusable types and behaviours respectively.A function can accept arguments and can return a value. Anonymous functions can be assigned to variables and passed around. Code blocks (including function bodies) are defined within
{…}
. Control expressions includeif-else
,while
,loop
,if-let
andwhile-let
. However, parentheses are rarely used around these expressions. -
Is Rust an object-oriented or a functional language? Rust combines some aspects of both object-oriented and functional programming paradigms.
Rust provides
struct
andenum
types that contain data and behaviour. Behaviour is specified usingimpl
blocks. Rust provides encapsulation at many levels: modules, types, functions and methods. By default these are private and they're made public usingpub
keyword. Thus, developers can change internal implementations without affecting their users who use the public API.Inheritance is widely used in OOP but this is not supported in Rust. However, Rust uses traits to achieve code reuse. Trait objects help achieve dynamic dispatch or polymorphism. In fact, trait objects are closer to OOP's objects.
Rust's
enum
and associated pattern matching are similar to algebraic data types seen in functional languages. Closures are supported, that is, we can define anonymous functions that capture their environments, save them into variables and use as function arguments. The use of iterators to process a sequence of items is also influenced by functional programming. -
Who has adopted Rust so far? Apart from Mozilla that created Rust, the language has been adopted by many companies including Dropbox, Coursera, Figma, npm, Microsoft, Cloudflare, Facebook, Amazon and Discord. Adoption spans standalone software, cloud-hosted software, edge software and even embedded devices.
Dropbox preferred Rust's compile-time checks on static types over Python's dynamic typing. Rust provided high concurrency required by their file synchronization engine. At Coursera, user-submitted code is executed within Docker containers. Being a low-level and type-safe language, Rust offered Coursera more security than C. Figma rewrote its multiplayer syncing engine in Rust for better performance because the older TypeScript code suffered from garbage collection. At Discord, Rust is used for both client-side (via WASM) and server-side logic. Cloudflare is preferring Rust over C at the cloud edge because of its better memory safety.
Facebook's source control team chose Rust over C++ for its higher reliability. By 2019, Rust was widely adopted in Facebook, first for developer tooling and later for small services.
For embedded development, many RTOSes, CPU architectures and boards offer APIs for Rust. Sensirion, Airborne Engineering Ltd. and 49nord are some adopters in the embedded space.
-
What are some criticisms of Rust? Rust has its fair share of criticism. Among them are its complex syntax and a steep learning curve; slow compilations; limited choice of compilers and target architectures compared to C; limited tooling; limited or immature third-party libraries; unsafe and difficult integrations with other languages. Developers can misuse
unsafe
blocks. While its positioned as a systems programming language, not every programming is of this nature.Rust is too verbose with syntax such as
Vec<Rc<RefCell<Box<Trait>>>>
. There are five types of pointers and it's not trivial to select the right one. Rust lacks a strict description of its semantics. This means that formal verification is not possible. The use of downloadable crates can lead to complex dependency trees. Some consider Rust macros a design mistake or an afterthought to cover up other mistakes.While performant, Rust is not much better than Go, Java, Scala or Haskell. While C++ is not type safe like Rust, C++ has many static and dynamic analyzers to achieve this. C continues to be better in many aspects: highly portable, formally specified, many implementations, stable ABI. For concurrency and simplicity, some developers prefer Go.
-
What tools are available for Rust programming? Rust tooling is improving all the time. Beginners can note the following:
- Installation: Rust can be installed or updated using
rustup
. This can also install optional components such asclippy
(linter) andrustfmt
(code formatter). - Compiler: The main compiler for Rust is
rustc
. It's based on LLVM compiler toolchain and therefore can target various architectures already supported by LLVM. The executable program or package at the output of the compiler is called a crate. The toolsccache
can reduce compile times. - IDE: VSCode, IntelliJ and Vim are some IDEs that support Rust, often via the Rust Language Server (RLS).
- Package Manager: Projects typically have dependencies. Cargo is the package manager that fetches project dependencies from a central repository. Cargo is also a build system. After downloading the right packages, it can invoke the
rustc
compiler. - Package Registry: Crates are typically published at
crates.io
. By default, Cargo searchescrates.io
for dependent packages. - Documentation: The tool for this is
rustdoc
, which can also be invoked using Cargo. Documentation is generated from doc comments in source files or standalone Markdown files.
- Installation: Rust can be installed or updated using
Milestones
Graydon Hoare at Mozilla, who usually works on compilers and tools, starts working on Rust as a hobby project. Later, when he shows this to his manager, Mozilla takes interest in this new language. The idea is to see if Rust could be used to rewrite their browser stack in a more safer and more concurrent way than possible with C++. In a later interview, Hoare notes that Rust's target audience was "frustrated C++ developers".
2014
2017
Mozilla's Firefox Quantum now uses a CSS engine written entirely in Rust. Called Stylo, it replaces 160,000 lines of C++ with 85,000 lines of Rust. Due to Rust's excellent support for concurrency, the new engine speeds up page styling. Styling of child DOM elements is done concurrently. Developers could write code without worrying too much about safety. They found Rust's panics easier to deal with than C++ segmentation faults.
In the annual StackOverflow developer survey, Rust is the most-loved programming language for the fifth year in a row. These are from survey participants who use Rust and will continue to use it. Rust is in fifth position as the most-wanted language. In July, Rust rises to 18th place in the TIOBE index from 33rd place just a year earlier.
2021
2021
Sample Code
References
- Asay, Matt. 2021. "Rust, not Firefox, is Mozilla’s greatest industry contribution." Accessed 2022-02-06.
- Avram, Abel. 2012. "Interview on Rust, a Systems Programming Language Developed by Mozilla." InfoQ, August 3. Accessed 2022-02-06.
- Bhat, Manjunath. 2021. "Why 2021 Will be a ‘Rust’y Year for System Programmers." Blog, Gartner, January 3. Accessed 2022-02-06.
- Blandy, Jim and Jason Orendorff. 2018. "Programming Rust: Fast, Safe Systems Development." O'Reilly. Accessed 2022-02-06.
- Burgdorf, Christoph. 2014. "Rethinking Systems Programming." Slides, May. Accessed 2022-02-06.
- Bychkov, Andrey and Vsevolod Nikolskiy. 2021. "Rust Language for Supercomputing Applications." In: Voevodin V., Sobolev S. (eds), Supercomputing, RuSCDays 2021, Communications in Computer and Information Science, vol. 1510. Springer, Cham. Accessed 2022-02-06.
- DeVault, Drew. 2019. "Rust is not a good C replacement." Blog, March 25. Accessed 2022-02-06.
- Dieter, Henk. 2020. "Why Rust is a great fit for embedded software." Blog, Tweede golf, June 12. Accessed 2022-02-06.
- Dreimanis, Gints. 2020. "Introduction to Rust." Blog, Serokell, August 19. Accessed 2022-02-06.
- Dreimanis, Gints. 2020a. "9 Companies That Use Rust in Production." Blog, Serokell, November 18. Accessed 2022-02-06.
- Fulton, Kelsey R., Anna Chan, Daniel Votipka†, Michael Hicks, and Michelle L. Mazurek. 2021. "Benefits and Drawbacks of Adopting a Secure Programming Language: Rust as a Case Study." Seventeenth Symposium on Usable Privacy and Security (SOUPS 2021), USENIX, August 8-10. Accessed 2022-02-06.
- Goregaokar, Manish. 2017. "Fearless Concurrency in Firefox Quantum." Rust Blog, November 14. Accessed 2022-02-06.
- István, Szmozsánszky. 2015. "Diving into Rust for the first time." Mozilla Hacks, May 15. Accessed 2022-02-06.
- Jones, M. Tim. 2018. "Why you should learn the Rust programming language." IBM, March 8. Accessed 2022-02-06.
- Jung, Ralf, Jacques-Henri Jourdan, Robbert Krebbers, and Derek Dreyer. 2021. "Safe Systems Programming in Rust." Communications of the ACM, vol. 64, no. 4, pp. 144-152, April. Accessed 2022-02-06.
- Klabnik, Steve. 2016. "The History of Rust." Association for Computing Machinery (ACM), on YouTube, June 22. Accessed 2022-02-06.
- Klabnik, Steve and Carol Nichols. 2019. "The Rust Programming Language." No Starch Press, San Francisco. Accessed 2022-02-06.
- Kladov, Alexsey. 2020. "Why Not Rust?" September 20. Accessed 2022-02-06.
- Krill, Paul. 2020. "Rust language tops Stack Overflow survey." InfoWorld, June 2. Accessed 2022-02-06.
- Madunuwan, Dumindu. 2021. "Why Rust?" Learning Rust, April 27. Accessed 2022-02-06.
- Matthews, Brenden. 2019. "Programming in Rust: the good, the bad, the ugly." Hackernoon, March 11. Accessed 2022-02-06.
- Melanhovich, Eax. 2015. "Criticizing the Rust Language, and Why C/C++ Will Never Die." Blog, PVS-Studio, May 12. Accessed 2022-02-06.
- Meta. 2021. "A brief history of Rust at Facebook." Engineering at Meta, April 29. Accessed 2022-02-06.
- Milano, Matt. 2020. "The Rust Programming Language: Its History and Why It Matters." Talentopia, August 10. Accessed 2022-02-06.
- Mozilla. 2021. "Mozilla Welcomes the Rust Foundation." dist://ed, Mozilla Blog, February 8. Accessed 2022-02-06.
- Mozilla Research. 2022. "Rust language." Mozilla Research. Accessed 2022-02-06.
- Mozilla Wiki. 2021. "Areweyet." Wiki, Mozilla, April 20. Accessed 2022-02-06.
- Mulders, Michiel. 2020. "When to use Rust and when to use Go." Blog, LogRocket, September 28. Accessed 2022-02-06.
- Perkel, Jeffrey M. 2020. "Why scientists are turning to Rust." Nature, December 1. Updated 2020-12-11. Accessed 2022-02-06.
- Rust. 2022. "Production users." Rust. Accessed 2022-02-06.
- Rust. 2022a. "Embedded devices." Rust. Accessed 2022-02-06.
- Rust. 2022b. "Homepage." Rust. Accessed 2022-02-06.
- Rust Cargo. 2021. "Why Cargo Exists." Section 2.1, The Cargo Book, May 18. Accessed 2022-02-06.
- Rust Cargo. 2021b. "Dependencies." Section 2.4, The Cargo Book, December 5. Accessed 2022-02-06.
- Rust Embedded. 2022. "Embedded Rust." rust-embedded/awesome-embedded-rust, on GitHub, January 27. Accessed 2022-02-06.
- Rustdoc. 2021. "What is rustdoc?" Section 1, The rustdoc book, January 26. Accessed 2022-02-06.
- Tung, Liam. 2020. "Programming language Rust's adoption problem: Developers reveal why more aren't using it." ZDNet, April 21. Accessed 2022-02-06.