The trifecta of Rust â safety, speed, and concurrency

Rust is not a revolutionary language with new cutting-edge features, but it incorporates a lot of proven techniques from older languages while massively improving upon the design of C++ in matters of safe programming.

The Rust developers designed Rust to be a general-purpose and multi-paradigm language. Like C++, it is an imperative, structured, and object-oriented language. Besides this, it inherits a lot from functional languages and also incorporates advanced techniques for concurrent programming.

In Rust, the typing of variables is static (because Rust is compiled) and strong. However, unlike Java or C++, the developer is not forced to indicate the types for everything as the Rust compiler is able to infer the types in many cases.

C and C++ are known to be haunted by a series of problems that often lead to program crashes or memory leaks which are notoriously difficult to debug and solve. Think about dangling pointers, buffer overflows, null pointers, segmentation faults, data races, and so on. The Rust compiler (called rustc ) is very intelligent and can detect all these problems while compiling your code, thereby guaranteeing memory safety during execution. This is done by the compiler by retaining complete control over memory layout, without needing the runtime burden of garbage collection (see Chapter 6, Pointers and Memory Safety). In addition, its safety also implies much less possibilities for security breaches.

Rust compiles native code like Go and Julia. However, in contrast to these two, Rust doesn't need runtime with garbage collection. In this respect, it also differs from Java JVM and the languages that run on the JVM, such as Scala and Clojure. Most other popular modern languages such as .NET with C# and F#, JavaScript, Python, Ruby, Dart, and so on, all need a virtual machine and garbage collection.

As one of its mechanisms for concurrency, Rust adopts the well-known actor model from Erlang. Lightweight processes called threads perform work in parallel. They do not share heap memory but communicate data through channels, and data races are eliminated by the type system (see Chapter 8, Concurrency and Parallelism). These primitives make it easy for programmers to leverage the power of many CPU cores that are available on current and future computing platforms.

The rustc compiler is completely self hosted, which means that it is written in Rust and can compile itself by using a previous version. It uses the LLVM compiler framework as its backend (for more information on LLVM compiler framework, go to http://en.wikipedia.org/wiki/LLVM) and produces natively executable code that runs blazingly fast because it compiles to the same low-level code as C++ (To see an example of its speed, go to http://benchmarksgame.alioth.debian.org/u64q/rust.php.).

Rust is designed to be as portable as C++ and run on widely used hardware and software platforms; at present, it runs on Linux, Mac OS X, Windows, FreeBSD, Android, and iOS. It can call C's code as simply and efficiently as C can call its own code, and conversely, C can also call Rust code (see Chapter 9, Programming at the Boundaries). The following is the logo of Rust:

Other Rust characteristics that will be discussed in more detail in later chapters are as follows:

Its variables are immutable by default (see Chapter 2, Using Variables and Types)

Enums (see Chapter 4, Structuring Data and Matching Patterns)

Pattern matching (see Chapter 4, Structuring Data and Matching Patterns)

Generics (see Chapter 5, Generalizing Code with Higher-order Functions and Parametrization)

Higher-order functions and closures (see Chapter 5, Generalizing Code with Higher-order Functions and Parametrization)

The interface system called traits (see Chapter 5, Generalizing Code with Higher-order Functions and Parametrization)

A hygienic macro system (see Chapter 7, Organizing Code and Macros)

Zero-cost abstractions, which means that Rust has higher-language constructs, but these do not have an impact on performance

In conclusion, Rust gives you ultimate power over memory allocation as well as removing many security and stability problems that are commonly associated with native languages.