Rust Learning Guide

Rust programming guide organized by category

πŸ’Ž Rust for Rubyists

πŸ“Œ

Variables & Immutability β€” Why Ruby's Free Reassignment Disappears

let, mut, shadowing, const β€” side-by-side with Ruby variables

In Ruby, variables can be reassigned anytime. Rust defaults to immutable. Comparing mut, shadowing, and const with Ruby code.

🏷️

Type System β€” What You Gain by Giving Up Dynamic Typing

Ruby duck typing vs Rust static type inference

Ruby uses duck typing. "If it quacks, it's a duck." Rust determines all types at compile time. Inference is powerful enough that you rarely annotate, but once set, types never change.

πŸ”§

Functions & Methods β€” What Changes When def Becomes fn

Return types, semicolons, and the familiar "last expression is the return" rule

Ruby def β†’ Rust fn. Argument types and return types must be explicit, and a single semicolon determines whether something is returned. Otherwise, surprisingly similar to Ruby.

πŸ—οΈ

Structs & impl β€” Ruby's class Splits Into Two Pieces in Rust

struct for data, impl for methods, trait for polymorphism

Ruby class bundles data and methods in one place. Rust separates struct (data) and impl (methods), using traits instead of inheritance for shared behavior.

🎯

Enums & Pattern Matching β€” A Powerful Evolution of Symbol and case/when

Rust enums can carry data β€” a concept Ruby doesn't have

Rust enums β€” the equivalent of Ruby Symbols or constant groups β€” can carry data in each variant. match is an enhanced case/when that requires handling every case.

❓

Option<T> β€” Surviving in a World Without nil

How Ruby's nil and &. operator translate to Rust

Ruby nil shows up anywhere. Rust has no nil. If a value might not exist, use Option<T> β€” the compiler forces you to handle None.

⚠️

Result & Error Handling β€” What Fills the begin/rescue Void

Rust handles errors as return values instead of exceptions

Ruby throws exceptions and catches with rescue. Rust has no exceptions. Instead, it returns Result<T, E> and propagates errors with the ? operator.

πŸ”„

Iterators & Closures β€” Where Rubyists Feel Most at Home

select/map/reduce just became filter/map/fold

Ruby Enumerable methods exist almost identically in Rust. Names differ slightly, chaining patterns are the same. The fastest adaptation area for Rubyists.

πŸ“

String vs &str β€” The Two Faces of Strings Ruby Doesn't Have

The difference between owned and borrowed strings

Ruby has one String. Rust has two: String (owned) and &str (borrowed). Confusing at first, but it connects to the core ownership concept.

πŸ“¦

Collections β€” When Array and Hash Become Vec and HashMap

Arrays and hashmaps in a type-fixed world

Ruby Array and Hash become Vec<T> and HashMap<K, V> in Rust. The biggest difference: they hold only one type.

πŸ—οΈ

Ownership β€” Doing What Ruby's GC Did for You

The core Rust concept Rubyists struggle with most

In Ruby, GC handles memory cleanup. In Rust, ownership rules replace GC. The core concept: moving a value makes the original disappear.

πŸ“¦

Cargo β€” Bundler + Rake Combined Into One

Gemfile β†’ Cargo.toml, bundle exec β†’ cargo run

Ruby uses Bundler (dependencies) and Rake (build/tasks) separately. Rust does everything with Cargo: project creation, build, test, dependency management, publishing.