Rust Tutorial

Rust - Introduction

Rust is a modern multi-paradigm programming language designed for performance and safety, especially safe concurrency. Rust is syntactically similar to C++, and amazingly fast like C and C++. Rust was originally designed by Graydon Hoare at Mozilla Research, with contributions from Dave Herman, Brendan Eich, and others. The first version of Rust was released in July, 2010. Some important features of Rust are given below:

Language Features:

Performance – Rust programming language does not have a Garbage Collector (GC) by design. This improves the performance at runtime.

Zero cost abstraction – In Rust, we can add abstractions without affecting the runtime performance of the code. It improves the code quality and readability of the code without any runtime performance cost.

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

Types Interface – 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. Variables assigned multiple times must be marked with the keyword mut.

Pattern matching – Rust provides the feature of pattern matching. In pattern matching, patterns are used in conjunction with the match expressions to give more control over the program's control flow. Following are the combinations of some patterns:

  • Literals
  • Variables
  • Arrays, enums, structs, or tuples
  • Wildcards
  • Placeholders

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, 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 & 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.