Cpp Cheatsheet

Rust Fundamentals

Overview

  • Writing Safe Rust gets you the following benefits instantly:
    • running UBSAN, ASAN, THREADSAN and RAII analysis at compile time, without the performance penalty at runtime
    • all bindings are consted by default unless they specifically opt out with let mut
    • all code you depend on is also analyzed under the same constraints by the compiler

Installation

  • LLVM is the default codegen backend but the experimental gcc has not yet stabilized
  • CodeLLDB is a reliable debugger setup

Basic Types

  • Raw pointers do exist but are rarely used. Their typed variants with added semantics and safety are preferred, like &.
  • Rust Strings do not have the Small String Optimization that C++ does
    • Try a drop in replacement like smallstr instead

Basic Types 2

  • Rust's println! semantics for non-numerics follow those of sprintf, but with {}:
    • %-10s to format a left aligned string padded to minimum 10 spaces becomes {:<10}
    • %04 to pad a number with zeros up to a width of 4 becomes {:04}, etc.
  • Rust does not have user defined literals so you need a macro to make let duration = 5_milliseconds; work in Rust.

Basic Types 3

  • A Rust enum is most similar to an std::variant
  • = and += like operators return the value that was set, whereas in Rust they do not.

Control Flow

  • This loop
const auto v = {1,2,3,4};
for (const auto &: list) {
    //...
}

is equivalent to

for value in &list {
    //...
}

Compound Types

Ownership and Borrowing

Error Handling

Collections

Iterators

Imports and Modules

Good Design Practices

Applied Rust

Methods and Traits

Rust I/O Traits

Generics

Lifetimes

Cargo Workspaces

Heap Allocation (Box and Rc)

Shared Mutability (Cell, RefCell, OnceCell)

Thread Safety (Send/Sync, Arc, Mutex)

Closures and the Fn/FnOnce/FnMut traits

Spawning Threads and Scoped Threads

Advanced Rust

Advanced Strings

Building Robust Programs with Kani

Dealing with Unwrap

Debugging Rust

Deconstructing Send, Arc, and Mutex

Dependency Management with Cargo

Deref Coercions

Design Patterns

Documentation

Drop, Panic and Abort

Dynamic Dispatch

Macros

Property Testing

Rust Projects Build Time

Send and Sync

Serde

Testing

The stdlib

Using Cargo

Using Types to encode State

Rust and Web Assembly

WASM