Rust

Topics related to Rust:

Getting started with Rust

Rust is a systems programming language designed for safety, speed, and concurrency. Rust has numerous compile-time features and safety checks to avoid data races and common bugs, all with minimal to zero runtime overhead.

Loops

Tests

Strings

Macros

Cargo

  • At the moment, the cargo bench subcommand requires the nightly version of the compiler to operate effectively.

Option

Serde

Pattern Matching

When deconstructing a structure value, the field should be either of the form field_name or field_name: pattern. If no pattern is specified, an implicit binding is done:

let Point { x, y } = p;
// equivalent to
let Point { x: x, y: y } = p;

let Point { ref x, ref y } = p;
// equivalent to
let Point { x: ref x, y: ref y } = p;

1: Irrefutable pattern

Parallelism

Globals

File I/O

Traits

  • Traits are commonly likened to interfaces, but it is important to make a distinction between the two. In OO languages like Java, interfaces are an integral part of the classes that extend them. In Rust, the compiler knows nothing of a struct's traits unless those traits are used.

TCP Networking

Error handling

Generics

Closures and lambda expressions

Lifetimes

  • All references in Rust have a lifetime, even if they are not explicitly annotated. The compiler is capable of implicitly assigning lifetimes.
  • The 'static lifetime is assigned to references that are stored in the program binary and will be valid throughout its entire execution. This lifetime is most notably assigned to string literals, which have the type &'static str.

Modules

Auto-dereferencing

Conversion traits

  • AsRef and Borrow are similar but serve distinct purposes. Borrow is used to treat multiple borrowing methods similarly, or to treat borrowed values like their owned counterparts, while AsRef is used for genericizing references.
  • From<A> for B implies Into<B> for A, but not vice-versa.
  • From<A> for A is implicitly implemented.

Tuples

Signal handling

Ownership

  • In much older versions of Rust (before 1.0; May 2015), an owned variable had a type starting with ~. You may see this in very old examples.

Structures

Rust Style Guide

The official Rust style guidelines were available in the rust-lang/rust repository on GitHub, but they have recently been removed, pending migration to the rust-lang-nursery/fmt-rfcs repository. Until new guidelines are published there, you should try to follow the guidelines in the rust-lang repository.

You can use rustfmt and clippy to automatically review your code for style issues and format it correctly. These tools can be installed using Cargo, like so:

cargo install clippy
cargo install rustfmt

To run them, you use:

cargo clippy
cargo fmt

Iterators

Documentation

This section of the 'Rust Book' may contain useful information about documentation and documentation tests.

Documentation comments can be applied to:

  • Modules
  • Structs
  • Enums
  • Methods
  • Functions
  • Traits and Trait Methods

Arrays, Vectors and Slices

Unsafe Guidelines

Foreign Function Interface (FFI)

Object-oriented Rust

Panics and Unwinds

Panics don't always cause memory leaks or other resource leaks. In fact, panics typically preserve RAII invariants, running the destructors (Drop implementations) of structs as the stack unwinds. However, if there is a second panic during this process, the program simply aborts; at that point, RAII invariant guarantees are void.

Inline Assembly

Command Line Arguments

Associated Constants

GUI Applications

Regex

PhantomData

The Drop Trait - Destructors in Rust

Raw Pointers

  • Raw pointers are not guaranteed to point to a valid memory address and as such, careless usage may lead to unexpected (and probably fatal) errors.
  • Any normal Rust reference (eg. &my_object where the type of my_object is T) will coerce to *const T. Similarly, mutable references coerce to *mut T.
  • Raw pointers do not move ownership (in contrast to Box values that)

Operators and Overloading

Iron Web Framework

Bare Metal Rust

Futures and Async IO

Primitive Data Types

Random Number Generation

There is built-in support for a RNG associated with each thread stored in thread-local storage. This RNG can be accessed via thread_rng, or used implicitly via random. This RNG is normally randomly seeded from an operating-system source of randomness, e.g. /dev/urandom on Unix systems, and will automatically reseed itself from this source after generating 32 KiB of random data.

An application that requires an entropy source for cryptographic purposes must use OsRng, which reads randomness from the source that the operating system provides (e.g. /dev/urandom on Unixes or CryptGenRandom() on Windows). The other random number generators provided by this module are not suitable for such purposes.

rustup

Custom derive: "Macros 1.1"

Boxed values