Book Image

Rust Essentials

By : Ivo Balbaert
Book Image

Rust Essentials

By: Ivo Balbaert

Overview of this book

<p>Starting by comparing Rust with other programming languages, this book will show you where and how to use Rust. It will discuss primitive types along with variables and their scope, binding and casting, simple functions, and ways to control execution flow in a program.</p> <p>Next, the book covers flexible arrays, vectors, tuples, enums, and structs. You will then generalize the code with higher-order functions and generics applying it to closures, iterators, consumers, and so on. Memory safety is ensured by the compiler by using references, pointers, boxes, reference counting, and atomic reference counting. You will learn how to build macros and crates and discover concurrency for multicore execution.</p> <p>By the end of this book, you will have successfully migrated to using Rust and will be able to use it as your main programming language.</p>
Table of Contents (17 chapters)
Rust Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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.

Comparison with other languages

Dynamic languages such as Ruby or Python give you the initial coding speed, but you pay the price later when you have to write more tests, runtime crashes, or even production outages. The Rust compiler forces you to get a lot of things right at compile-time, which is the least expensive place to identify and fix bugs.

Rust's object orientation is not that explicit or evolved as common object-oriented languages such as Java, C#, and Python as it doesn't have classes. Compared with Go, Rust gives you more control over memory and resources, so lets you code on a lower level. Go also works with a garbage collector, and it has no generics or a mechanism to prevent data races between its goroutines that are used in concurrency. Julia is focused on numerical computing performance; it works with a JIT compiler and doesn't give you that low-level control that Rust gives.