Rust for bioinformaticians (under construction)

An intensive course teaching Rust to 4th-year biology students who have already taken one introductory programming course (R, Python, or — at worst — MATLAB). The unit of design is the concept ↔︎ biology pairing: every Rust feature is introduced together with a biological problem that motivates it.

Where to start

  • Audience — who this course is for, and what we can and cannot assume they already know.

Before you start

Exercises by day

Day 1 — Toolchain, scalars, control flow, functions

  1. Concepts — reference companion to the slides
  2. Quiz: scalar types and ranges
  3. GC content
  4. Complement a single base
  5. Count A, C, G, T, and other
  6. Phred quality scores
  7. Hamming distance

Day 2 — Ownership, borrowing, strings, slices, bytes

  1. Concepts — reference companion to the slides
  2. Quiz: ownership and borrowing
  3. Reverse complement
  4. Reverse complement into a buffer
  5. k-mers as slice views
  6. Quality trimming as a slice view

Day 3 — Structs, enums, iterators, recursion, error handling

  1. Concepts — reference companion to the slides
  2. Quiz: enums, Option, and Result
  3. Genomic interval
  4. Parsing a region string with Result
  5. A codon table with Option
  6. Counting k-mers with a HashMap
  7. A phylogenetic tree with recursion
  8. Sequence alignment with dynamic programming

Day 4 — I/O, crates, modules, plotting, zip

  1. Concepts — reference companion to the slides
  2. Quiz: I/O, crates, and error handling
  3. Parsing BED by hand
  4. Per-contig stats with noodles-fasta
  5. Filter FASTQ with noodles-fastq
  6. Read-length histogram with plotters
  7. Bundling outputs into a .zip

Day 5 — Tests, --release, parallelism, ecosystem

  1. Concepts — reference companion to the slides
  2. Quiz: tests, --release, and parallelism
  3. Use tests to find the bug
  4. --release is a 50× free lunch
  5. Parallel GC content with rayon

Day 6 — Rust for the web

A pure-Rust web app: Yew frontend (compiled to WebAssembly), axum backend, shared types crate — all in one cargo workspace.

  1. Concepts — reference companion to the slides
  2. Quiz: Yew and WebAssembly
  3. Hello Yew
  4. Reactive sequence statistics
  5. Fetch from the backend
  6. Capstone — GC sliding window

Day 7 — SQL and databases

Modelling data in relational form, querying it from the sqlite CLI, then reading and writing the same database from Rust.

  1. Concepts — reference companion to the slides
  2. Quiz: SQL and SQLite
  3. Your first SQLite database
  4. A second table and a join
  5. Reading a SQLite database from Rust
  6. Writing a SQLite database from Rust
  7. Joining two tables from Rust

Day 8 — Traits

The Rust mechanism for shared interfaces — impl Trait for Type, generic functions with trait bounds, and Box<dyn Trait> for heterogeneous collections.

  1. Concepts — reference companion to the slides
  2. Quiz: traits, generics, and dispatch
  3. Implement Display for GenomicInterval
  4. A custom trait with multiple impls
  5. A generic function with a trait bound
  6. A Box<dyn> heterogeneous collection

Day 9 — Algorithms and complexity

A short day on data-structure complexity (Big-O) and how to recognise it in code — two diagnostic quizzes that drive the lecture.

  1. Concepts — reference companion to the slides
  2. Quiz: data-structure complexity
  3. Quiz: complexity from code

Day 10 — Working with matrices

Dense and sparse matrix representations (ndarray, sprs), iterative linear-system solvers, and Markov chains via power iteration.

  1. Concepts — reference companion to the slides
  2. Quiz: dense, sparse, and iterative
  3. Loading and operating on a dense matrix
  4. Sparse representations — memory comparison
  5. Implement Gauss-Seidel
  6. Markov chain — power iteration vs steady state

Day 11 — GPUs and tensors with Burn

GPU-aware tensors via the Burn framework, with every Rust example paired against the equivalent PyTorch / NumPy. Builds up to PCA via nalgebra, a PWM motif scanner as Conv1d, and a small trainable promoter classifier.

  1. Concepts — reference companion to the slides
  2. Quiz: tensors, backends, and shapes
  3. Tensors on any backend
  4. One-hot encoding DNA
  5. GC content across a batch
  6. Pairwise distance matrix
  7. PCA with nalgebra
  8. 1-D convolutional motif scanner
  9. Train a promoter classifier

Day 12 — GPU compute kernels with wgpu

The “how” beneath day 11’s “what” — write WebGPU Shading Language compute shaders by hand and dispatch them with wgpu. Several exercises revisit earlier problems (GC content, reverse complement, pairwise distances) as raw GPU kernels.

  1. Concepts — reference companion to the slides
  2. Quiz: wgpu compute kernels
  3. Vector add — your first kernel
  4. SAXPY — uniforms for scalars
  5. GC content on the GPU (revisit Day 1 / Day 5)
  6. Reverse complement on the GPU (revisit Day 2)
  7. Parallel reduction (sum)
  8. Pairwise distance matrix (revisit Day 11 ex 4)

Working with the exercises

Each exercise pairs a short .qmd walkthrough with one or two real cargo projects:

  • The walkthrough holds the prose, the hints (as collapsible callouts), and a reference solution snippet.
  • The sibling ex-<name>/ cargo project contains code that the Rust compiler will actually check, plus a cargo test spec that pins down the expected behaviour.
  • The ex-<name>-solution/ cargo project contains the reference solution as runnable code.

Building this site

make           # render the whole site to _site/
make preview   # start a live-reload preview server
make clean     # delete _site/ and run `cargo clean` in every exercise crate

You will need Quarto and a recent Rust toolchain installed.