FABRKNT
Reth Fundamentals — Your First Steps with Alloy
Working with Alloy
Lesson 1 of 11·CONTENT12 min25 XP

Treat this page as a workbench, not a blog post. The goal is to extract a reusable mental model from the source and carry it into the rest of the Fabrknt stack.

Course
Reth Fundamentals — Your First Steps with Alloy
Lesson role
CONTENT
Sequence
1 / 11

Lesson 1 — Rust: ownership and borrowing in 5 minutes

Question

As soon as you start writing Alloy, you hit ownership — Rust's defining feature and the first wall. Perfect understanding isn't the goal; the target is "can recall the rules while reading code".

Principle (minimum model)

  • Ownership = compile-time memory management. C/C++ humans manage memory; Java/JS GCs manage it; Rust the compiler verifies "who owns and when to free" → no GC, no double-free, no data races.
  • Three rules. (1) Every value has exactly one owner. (2) When the owner goes out of scope, the value is dropped. (3) Values are either moved or borrowed.
  • Borrows & and &mut. & = read (many at a time) / &mut = write (one at a time, exclusively) — data races prevented at compile time.
  • &str = a borrowed string. A String owns; a &str borrows a slice. &str in a function signature means "read-only, no ownership needed".
  • Three common Alloy patterns. "...".parse()? (string parse + error propagation) / provider.get_balance(&address).await? (borrow) / let mut signer = ... (mutable).

Worked example + steps

Rust: ownership and borrowing in 5 minutes

The first wall in Rust is ownership. You don't need to master it to read Alloy code — you need to recognize the rules when they show up.

1. Why ownership exists

C/C++ leaves memory management to you. Java/JS hides it behind a garbage collector. Rust takes a third path: the compiler tracks ownership at compile time and rejects programs that misuse memory.

The result:

  • No garbage collector, no manual free
  • Double-frees and use-after-free become compile errors
  • Data races between threads become compile errors

That's the property that makes Rust uniquely good at "money-handling code."

2. The three rules

1. Each value has exactly one owner
2. When the owner goes out of scope, the value is dropped
3. Values are either moved (transferring ownership) or borrowed

3. Move

let s1 = String::from("hello");
let s2 = s1;          // ownership moves s1 → s2
// println!("{}", s1); // ❌ error: s1 no longer valid
println!("{}", s2);   // OK

4. Borrowing with & (read-only reference)

If you only want to read, don't take ownership — borrow:

fn print_addr(addr: &String) {
    println!("{}", addr);
}

let a = String::from("0xABCD...");
print_addr(&a);   // & lends a
print_addr(&a);   // a still owns it; lend as many times as you want

5. &mut (mutable reference)

fn append_suffix(s: &mut String) {
    s.push_str("...");
}

let mut a = String::from("Hello");
append_suffix(&mut a);

The rule at any moment, you can have either:

  • Multiple & (read-only) OR
  • Exactly one &mut (mutable)

This is what makes data races a compile error.

6. What is &str, really?

&str is just a borrow of a string. String owns the characters; &str is a window into them.

let owned: String = String::from("Hello, Alloy");
let borrowed: &str = &owned;

When a function takes &str, it's saying: "I just want to read — I don't need to own this."

7. Patterns you'll see in Alloy

// .parse()? — parse and propagate errors
let url = "https://eth.llamarpc.com".parse()?;

// & — pass a borrow, not ownership
provider.get_balance(&address).await?;

// mut — declare a variable as mutable
let mut signer = PrivateKeySigner::random();

These all flow from the rules above. When you spot &, &mut, or mut in code, the compiler is enforcing one of the three rules behind the scenes.

Cheat sheet

SymbolMeaning
xowned
&xborrow (read)
&mut xborrow (write)
mut xvariable can be reassigned

You won't fully internalize ownership until you've fought a few compile errors. That's normal. Move on — the next lesson exercises this.

Summary (3 lines)

  • Three ownership rules = one owner, dropped on scope exit, move or borrow. Compile-time memory management means no GC and no data races.
  • & read (many) + &mut write (one) prevent races; &str is a String borrow.
  • Perfect understanding not needed — "recall while reading" is. Next lesson: Alloy primitives and signing.