Skip to content

Instantly share code, notes, and snippets.

@jproyo
Forked from monadplus/rust_nutshell.md
Created September 23, 2022 09:06
Show Gist options
  • Save jproyo/dd0f2ec129e2688ba41261fd8d88a776 to your computer and use it in GitHub Desktop.
Save jproyo/dd0f2ec129e2688ba41261fd8d88a776 to your computer and use it in GitHub Desktop.
Rust in a Nutshell

Rust in a Nutshell

Table of Contents

Rust: The Language

Features:

  • Compiled to machine code
  • Strong, static typing
  • Imperative, with functional aspects
  • Can do both high-level and low-level programming
  • No garbage collection or runtime
  • Control allocation
  • Safe (e.g. no null pointers, correct concurrency...)
  • Cross-compilation (WebAssembly!)
  • Works with traditional tools (e.g. perf, gdb, valgrind...)
  • Modern type system
  • Long-term viability: Mozilla, Dropbox, CloudFlare, Microsoft, Google, Amazon, Facebook, Atlassian, npm

Drawbacks:

  • Learning curve: borrow checker
  • Ecosystem: still growing
  • Compile times are great compared to Haskell, but still slow compared to Go, C.
  • Vendor support (e.g. CUDA sdk, NVIDIA sdk), no C++ integration.
  • Windows ? Linux and MacOS mainly

Borrow Checker

Macros

  • Declarative macros e.g. vec!
  • Procedural macros e.g. #[derive(MyTrait)]
  • Example

Resources:

Parallelism & Concurrency

Example

Transaction Engine

Dependency injection

A common practice is to use traits and parametric structs.

Use mockall for testing.

Functional Programming in Rust

  • Algebraic Data Types + patterns
  • Efficient Generics (monomorphization)
  • Higher-order functions
  • Iterators
  • Option(Maybe) and Result(Either)
  • No Monads, but we have ?
  • sequence and traverse

Not recommended:

Low-level Programming in Rust

Rust is sold (or was sold) as an embedded programming language.

FFI with C

Lots of tools:

See example

Cross-compilation

Rust can cross-compile to a big number of platforms: x86, arm, mips, powerpc, risc-v... with just cargo build --target=x86_64-apple-darwin

Project Structure

Ecosystem

  • rustc
  • cargo
  • rustfmt
  • clippy

Rustup

rustup

  • Any toolchain(channel + version), any target(cross-compilation), any component(clippy, lsp)!
  • rustup run stable rustc --version (or rustc +stable --version)
  • rustup default nightly
  • rustup component add rust-docs

IDE support

  • rust-analyzer: rustup +nightly component add rust-analyzer-preview
  • Vscode, vim/neovim, emacs, etc.

Documentation

Libraries

How to pick a library

Example: picking an HTTP Server

Candidates:

Community

Good practices

Learning Rust

Start with The Rust Book and see the other resources below.

More resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment