Rust is:
- low level given direct access to memory
- Yet high level given toolset provided

Cargo is built in: completely memory safe


Rust doc
- Tool that ships with compiler Rustc.
- Generates documentation for modules when passed module

// inline comments
/// rust doc will parse comments into documentation from markdown. Only public members (c/f private). Will also parse standalone markdown

Cargo doc command
Run through every module in every package in a project, and if internal packages automatically build those project docs
Doesn't run on other markdown files


rustfmt (Rust format)


Data structures fundamental
Can associate behaviours with structures?
Classes in classical languages like C++ or Java have ability to associate behaviour with any given data type (method)
Inheritance also common in classical languages. Or prototypal inheritance as in JavaScript

Rust uses traits (or mixins): extend the functionality of a given data type with another definition of how behaviour should work. Let's behaviour and data structures be orthogonal to each other.

Classical inheritance also coming in Rust.

2 basic type constructors in Rust: struct and enum (also tuples).

Struct are straight forward data type: can associate any type of data with them (analogous to struct type in C). Can also associate implementations with them (can have behaviour).

Members can be primatives, other structs, tuples, enums or function members.

Main difference between struct in Rust and class in C++ or Python is they don't have inheritance.

Rust makes no distinction between header and source files. Instead uses documentation tools rather than read header files to see how piece of data works.

Implementations live in implementation blocks (denoted impl keyword).

Access methods on the struct with ::

Once have an instance, use dot notation to access member.


Ownership core question in Rust because language manages memory. And all languages: shared mutable state evil.

Knowing state of given piece of data.

Problem of shared mutable state why hard rule against global variables exists: hard to reason about.

In Rust, can have any number of immutable references to given piece of data at a time, however if you have any mutable reference to a data point, cannot have any other reference to it. (Because wouldn't know if data would change).

Achieves this via notion of borrowing and moving and at compilation via borrow checker.

When borrow data get a reference (don't own data, just access).

When data moved, new function has ownership.

Memory belongs to scopes in Rust.

Define and pass references using & character (like in C). De-reference with star operator (let's you assign to references to the element itself- only mutable references).

Moved happen whenever use non-reference types in function calls. In C always pass be value when makes sense. In Rust often pass by reference instead because don't want to move ownership.



Pattern matching