Rust cheat sheet (2022)
Please refer to Don Bright’s original rust-lang-cheat-sheet which I based much of this content off, as I work through The Book. Rust in a nutshell Documentation Toolchain Mutability basic Types and variables Operators Run time errors, Crashing, panic, except, unwrap, Option, Result Printing loop, while and for Concurrency, parallel processing Functions and closures Unit tests, integration tests Documentation If, conditionals, patterns, match, control flow Ownership, Borrowing, References, Lifetimes Arrays, Slices, Ranges Structs Enums Collections, Key-value pairs, Sets Macros Little programs Guess a number Cool references Rust in a nutshell Syntax similar to C Ownership of memory enforced at compile time Statically linked Functional-ish, generic-ish, not so objecty Control flow using patterns, match keyword Packages: ‘cargo’ command, https://crates.io Testing: cargo test, #[test] Concurrency: ownership, mutability, channels, mutex, crossbeam + Rayon packages Auto formatter: rustfmt filename.rs Compiler engine: LLVM Raw pointers, low level: unsafe{} keyword Documentation rustup doc for offline docs! doc.rust-lang.org standard library, primitive types, standard macros docs.rs for package documentation Toolchain $ rustup.sh # install rust, see rust-lang.org for details $ cargo new myproj # start new executable project $ cargo new --bin myproj # as above $ cargo new --lib myproj # start new library project $ cd myproj # cd into the new directory $ ls -lR # list our skeleton of files src/main.rs # main.rs, has main() entry point Cargo.toml # Cargo.toml defines packaging $ $EDITOR Cargo.toml # add dependencies and other details $ cargo build # downloads dependencies + builds main.rs $ cargo build --release # release build $ cargo check # make sure code compiles, without binary outputs $ cargo update # ignore Cargo.lock and figure out latest versions $ cargo run # runs program created from main.rs $ cargo doc --open # local web based doc $ cargo test # runs tests (in parallel by default) $ cargo test -- --test-threads=1 # run tests one at a time $ cargo test -- --nocapture # run tests, show output $ cargo run --example fundemo -- --argtodemo # run example with argument $ rustc --explain E0384 # eli5 what an error code means Mutability basic let x = false; // all variable bindings are immutable by default x = true; // compile error: can't change an immutable binding let mut p = false; // "mut" designates a binding as mutable p = true; // ok, mutable binding can change; Types and variables Rust provide two compound types; arrays and tuples. ...