Skip to content

Instantly share code, notes, and snippets.

@monadplus
Last active March 1, 2024 04:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save monadplus/b3b0bbcd1f06ad175f435c9c3584a374 to your computer and use it in GitHub Desktop.
Save monadplus/b3b0bbcd1f06ad175f435c9c3584a374 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