Skip to content

Commit b42c7a9

Browse files
committed
docs: add insights on Rust ownership, borrowing, traits, and lifetimes to AGENTS.md
1 parent 635deee commit b42c7a9

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

AGENTS.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,58 @@ This repository contains Python bindings for Rust's DataFusion.
2828
- The repository mixes Python and Rust; ensure changes build for both languages.
2929
- If adding new dependencies, update `pyproject.toml` and run `uv sync --dev --no-install-package datafusion`.
3030

31+
## Rust insights
32+
33+
Below are a set of concise mental-model shifts and expert insights about Rust that are helpful when developing and reviewing the Rust parts of this repository. They emphasize how to think in terms of compile-time guarantees, capabilities, and algebraic composition rather than just language ergonomics.
34+
35+
1. Ownership → Compile-Time Resource Graph
36+
37+
> Stop seeing ownership as “who frees memory.”
38+
> See it as a **compile-time dataflow graph of resource control**.
39+
40+
Every `let`, `move`, or `borrow` defines an edge in a graph the compiler statically verifies — ensuring linear usage of scarce resources (files, sockets, locks) **without a runtime GC**. Once you see lifetimes as edges, not annotations, you’re designing **proofs of safety**, not code that merely compiles.
41+
42+
2. Borrowing → Capability Leasing
43+
44+
> Stop thinking of borrowing as “taking a reference.”
45+
> It’s **temporary permission to mutate or observe**, granted by the compiler’s capability system.
46+
47+
`&mut` isn’t a pointer — it’s a **lease with exclusive rights**, enforced at compile time. Expert code treats borrows as contracts:
48+
49+
* If you can shorten them, you increase parallelism.
50+
* If you lengthen them, you increase safety scope.
51+
52+
3. Traits → Behavioral Algebra
53+
54+
> Stop viewing traits as “interfaces.”
55+
> They’re **algebraic building blocks** that define composable laws of behavior.
56+
57+
A `Trait` isn't just a promise of methods; it’s a **contract that can be combined, derived, or blanket-implemented**. Once you realize traits form a behavioral lattice, you stop subclassing and start composing — expressing polymorphism as **capabilities, not hierarchies**.
58+
59+
4. `Result` → Explicit Control Flow as Data
60+
61+
> Stop using `Result` as an error type.
62+
> It’s **control flow reified as data**.
63+
64+
The `?` operator turns sequential logic into a **monadic pipeline** — your `Result` chain isn’t linear code; it’s a dependency graph of partial successes. Experts design their APIs so every recoverable branch is an **encoded decision**, not a runtime exception.
65+
66+
5. Lifetimes → Static Borrow Slices
67+
68+
> Stop fearing lifetimes as compiler noise.
69+
> They’re **proofs of local consistency** — mini type-level theorems.
70+
71+
Each `'a` parameter expresses that two pieces of data **coexist safely** within a bounded region of time. Experts deliberately model relationships through lifetime parameters to **eliminate entire classes of runtime checks**.
72+
73+
6. Pattern Matching → Declarative Exhaustiveness
74+
75+
> Stop thinking of `match` as a fancy switch.
76+
> It’s a **total function over variants**, verified at compile time.
77+
78+
Once you realize `match` isn’t branching but **structural enumeration**, you start writing exhaustive domain models where every possible state is named, and every transition is **type-checked**.
79+
80+
Stop seeing `Option` as “value or no value.” Instead, see it as a lazy computation pipeline that only executes when meaningful. These combinators turn error-handling into data flow: once you think of absence as a first-class transformation, you can write algorithms that never mention control flow explicitly—and yet, they’re 100% safe and analyzable by the compiler.
81+
82+
3183
## Refactoring opportunities
3284
- Avoid using private or low-level APIs when a stable, public helper exists. For example,
3385
automated refactors should spot and replace uses:

0 commit comments

Comments
 (0)