Skip to content
hoare-rust-origins logo

Graydon Hoare Style Guide

hoare-rust-origins

Write Rust code informed by Graydon Hoare's original vision for Rust. Emphasizes memory safety without GC, practical systems programming, and learning from C/C++ mistakes. Use when designing safe systems code or understanding Rust's fundamental choices.

SKILL.md

Full skill instructions

Graydon Hoare Style Guide

Overview

Graydon Hoare created Rust in 2006 as a personal project at Mozilla, driven by frustration with memory bugs in Firefox. His goal: a language as fast as C++ but safe by default. Rust's core innovation—ownership-based memory management—came from this vision.

Core Philosophy

"Rust is a systems programming language focused on safety, speed, and concurrency."

"Memory safety and thread safety are the same problem, approached from different angles."

Hoare designed Rust to eliminate entire classes of bugs that plague C and C++: use-after-free, double-free, data races, null pointer dereferences.

Design Principles

  1. Safety by Default: Unsafe operations require explicit unsafe blocks.

  2. No Garbage Collector: Memory management through ownership, not runtime overhead.

  3. Zero-Cost Abstractions: Safe code should be as fast as unsafe code.

  4. Compiler as Ally: The compiler catches bugs before runtime.

When Writing Code

Always

  • Let the borrow checker guide your design
  • Prefer stack allocation over heap when possible
  • Use Option<T> instead of null pointers
  • Use Result<T, E> for fallible operations
  • Make illegal states unrepresentable via types
  • Think about ownership before writing code

Never

  • Use unsafe without a clear safety comment
  • Leak memory (even though Rust allows it with mem::forget)
  • Ignore compiler warnings—they're often future errors
  • Use .unwrap() in library code (only in tests/​examples)
  • Create self-referential structs without understanding pinning

Prefer

  • &str over String for function parameters
  • &[T] over Vec<T> for read-only access
  • impl Trait over boxed trait objects when possible
  • Iterators over index-based loops
  • Pattern matching over if-else chains

Code Patterns

Ownership: The Foundation

// Ownership moves by default
fn main() {
    let s1 = String::from("hello");
    let s2 = s1;  // s1 is MOVED to s2
    // println!("{}", s1);  // ERROR: s1 no longer valid
    println!("{}", s2);  // OK
}

// Borrowing: temporary access without taking ownership
fn print_length(s: &String) {  // Borrows s
    println!("Length: {}", s.len());
}  // s goes out of scope, but since it's borrowed, nothing happens

fn main() {
    let s = String::from("hello");
    print_length(&s);  // Lend s
    println!("{}", s);  // s still valid!
}

Option Instead of Null

// BAD: Null pointer (not possible in safe Rust anyway)
// char* find(const char* haystack, char needle);  // C: returns NULL if not found

// GOOD: Option makes absence explicit
fn find(haystack: &str, needle: char) -> Option<usize> {
    haystack.chars().position(|c| c == needle)
}

fn main() {
    let text = "hello";
    match find(text, 'l') {
        Some(index) => println!("Found at {}", index),
        None => println!("Not found"),
    }
    
    // Or use combinators
    let index = find(text, 'l').unwrap_or(0);
    
    // Or the ? operator
    fn process(text: &str) -> Option<usize> {
        let index = find(text, 'l')?;  // Returns None if find returns None
        Some(index + 1)
    }
}

Result for Error Handling

use std::fs::File;
use std::io::{self, Read};

// Errors are values, not exceptions
fn read_file(path: &str) -> Result<String, io::Error> {
    let mut file = File::open(path)?;  // ? propagates error
    let mut contents = String::new();
    file.read_to_string(&mut contents)?;
    Ok(contents)
}

// Handle errors explicitly
fn main() {
    match read_file("config.txt") {
        Ok(contents) => println!("{}", contents),
        Err(e) => eprintln!("Error reading file: {}", e),
    }
}

Making Illegal States Unrepresentable

// BAD: Runtime checks needed
struct Connection {
    is_connected: bool,
    socket: Option<Socket>,
}

impl Connection {
    fn send(&self, data: &[u8]) {
        if self.is_connected {  // Runtime check!
            self.socket.as_ref().unwrap().write(data);
        }
    }
}

// GOOD: Type system enforces valid states
struct Disconnected;
struct Connected { socket: Socket }

impl Disconnected {
    fn connect(self, addr: &str) -> Result<Connected, Error> {
        let socket = Socket::connect(addr)?;
        Ok(Connected { socket })
    }
}

impl Connected {
    fn send(&mut self, data: &[u8]) -> Result<(), Error> {
        self.socket.write(data)  // Always valid!
    }
    
    fn disconnect(self) -> Disconnected {
        // socket dropped here
        Disconnected
    }
}

Zero-Cost Abstractions

// High-level iterator code...
let sum: i32 = (0..1000)
    .filter(|n| n % 2 == 0)
    .map(|n| n * n)
    .sum();

// ...compiles to the same machine code as:
let mut sum = 0i32;
for n in 0..1000 {
    if n % 2 == 0 {
        sum += n * n;
    }
}

// Abstractions have no runtime cost

Mental Model

Hoare designed Rust by asking:

  1. What bugs killed us in C++? Memory corruption, data races, null pointers
  2. Can the compiler catch these? Yes, with ownership tracking
  3. What's the performance cost? Zero—it's all at compile time
  4. Is this teachable? The borrow checker is strict but consistent

The Ownership Rules

  1. Each value has exactly one owner
  2. When the owner goes out of scope, the value is dropped
  3. You can have either:
    • One mutable reference (&mut T), OR
    • Any number of immutable references (&T)
  4. References must always be valid (no dangling)

These rules, enforced at compile time, prevent:

  • Use-after-free
  • Double-free
  • Data races
  • Null pointer dereferences

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View