type
status
date
slug
summary
tags
category
icon
password

Introduction

 
Below are my personal notes taken while learning the Rust language. Please refer to the original book and official Github for more detailed and concise information.
A collection of rust resources can be found here.
 
Here’s an example of how rust code and its compilation output would be presented in this article:

output:

Common Programming concepts

Variables and Mutability

Shadowing


output:

Data Types

rust is a statically typed language.
  • integer:
    • ix : signed x bits
      ux : unsigned

output:

output:

Compound Types


output:

output:

statement and expressions


output:

output:

Control Flow


output:

output:

Ownership

Stack and heap

  • stack: last in first out, fixed size, fast
  • heap: dynamic size, ptr, slower (focus)

output:

GC: Memory is returned once a variable gets out of scope.

Move

integer, float, bool, char, tuple, array are stored on stack, so they have Copy trait
Copy trait is not implemented for any type that implements the Drop trait.

output:

Reference and borrowing


output:

Simultaneous mutable references are not allowed, nor are mutable and immutable references.

output:

output:

string literals are slices (&str), and are therefore immutable.

Structs

Method Syntax

Enums and Pattern Matching

Options

match

if let

output:

Crates and Modules

Crates:
  • binary crate: executable, must have a src/main.rs
  • library crate: reusable code, must have a src/lib.rs
Packages: one or more crates
crate is root
(sub) Module declaration: inline, file, folder
private from parent by default unless pub
siblings can access each other’s private items
struct: need to specify public fields.
enum: once pub, all variants are pub.
Idiom: When importing with use
  • functions: import until parent module
  • structs, enums, and other items: specify the full path.
same name: differientiate by modules or as keyword.

Common Collections

Vector

Intialization

Reference a value


output:

Combine with enum to store multiple types

Iteration


output:

String

a wrapper around a vector of bytes, utf-8 encoded.

output:

String does not support indexing.

output:

HashMap


output:

output:

Error Handling

  • panic!: unrecoverable error. unwind the stack and clean up the data.
  • Result: recoverable error. return the error to the calling code.

? operator

? can only be used in functions that have a return type of Result or others that implements the FromResidual trait.
The return types have to match.

Creating Custom Types for Validation

Generic Types, Traits, and Lifetimes

Generic types


output:

when compiling, Rust performs monomorphization of the code that is using generics.

Traits

Default implementations can call other methods in the same trait,
even if those other methods don’t have a default implementation.
 
Trait as a parameter:
Trait bounds:
 
Using Trait Bounds to Conditionally Implement Methods:

Lifetimes

The Rust compiler has a borrow checker that compares scopes to determine whether all borrows are valid.
output:
Rust can’t tell whether the reference being returned refers to x or y.
 
Lifetime Annotations:
In practice, it means that the lifetime of the reference returned by the longest function is the same as the smaller of the lifetimes of the values referred to by the function arguments.
 
Lifetime Annotations in Struct Definitions:
Lifetime Elision:
rules:
  • each parameter that is a reference gets its own lifetime parameter;
  • if there is exactly one input lifetime parameter, it is assigned to all output lifetime parameters;
  • if there are multiple input lifetime parameters, but one of them is &self or &mut self, the lifetime of self is assigned to all output lifetime parameters.

Overview

Iterator and Closures

To be continued..
 
Visualizing Fourier Epicycles with Manim-CETemporal Difference Learning
  • Twikoo
  • Giscus