FABRKNT
Intro to Reth — Welcome to Rust Ethereum
Set Up Rust
Lesson 9 of 11·CONTENT12 min20 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
Intro to Reth — Welcome to Rust Ethereum
Lesson role
CONTENT
Sequence
9 / 11

Lesson 8 — Rust quick reference

Question

The Rust syntax you'll see in Reth / Revm / Alloy source, in one lesson. Not exhaustive — just the minimum set that gets you 80 % of the way to reading the source.

Principle (minimum model)

  • Variables. let x = 5; (immutable, default) / let mut y = 10; (mutable) / const PI: f64 = 3.14; (compile-time constant).
  • Functions. fn add(a: i32, b: i32) -> i32 { a + b }. The final expression is the return value (no ;). return is allowed but not idiomatic.
  • Ownership. let s1 = String::from("hello"); let s2 = s1; moves s1 (no longer usable). &s1 borrows. &mut s1 borrows mutably.
  • Result / Option / ?. Result<T, E> (success / failure) + Option<T> (some / none) + ? for early-return on error.
  • if is an expression. let x = if cond { a } else { b };. No ternary operator; if itself returns a value.
  • match. Pattern matching; all patterns must be covered; _ is the default arm.
  • struct / enum. struct Point { x: f64, y: f64 } / enum Color { Red, Green, Blue }. Methods in impl Point { fn new(...) -> Self { ... } }.
  • trait. Shared interface. impl Display for Point { ... } to implement. <T: Trait> for a generic bound.
  • async / await. async fn fetch() -> Result<...>; .await to wait. Runs on a Tokio runtime.
  • Macros. println! / vec! / format! / assert_eq! etc. The ! marks them. Compile-time expansion; more flexible than functions.

Worked example + steps

Rust quick reference

A fast tour of the Rust syntax you'll see throughout the course. There's no separate "learn Rust" course here — you pick up the language by going through the EVM material, with explanations the moment you need them.

1. Variables: let and let mut

Rust variables are immutable by default. Add mut to allow reassignment.

let x = 10;        // immutable
// x = 11;         // compile error

let mut y = 10;    // mutable
y = 11;            // OK

2. Primitive types

TypeMeaning
i32, i64signed integers
u32, u64, u128unsigned integers
booltrue / false
&strborrowed string (read-only, lightweight)
Stringowned string (mutable, heap-allocated)

The &str vs String distinction trips everyone up at first. For now: &str is "looking at someone else's house," String is "owning your own house." We'll do ownership properly in the next tier.

3. Functions

fn add(a: i64, b: i64) -> i64 {
    a + b   // no semicolon → it's an "expression" and becomes the return value
}
  • Parameters use name: type
  • Return type after ->
  • The trailing expression (no semicolon) is the implicit return

4. Methods

Rust values have methods you call with .:

let s = "0x123";
s.starts_with("0x");      // true / false
s.len();                  // 5
"hello".to_uppercase();   // "HELLO"

5. Control flow: if / else

let n = 7;
if n % 2 == 0 {
    println!("even");
} else {
    println!("odd");
}

if is itself an expression, so it can produce a value:

let parity = if n % 2 == 0 { "even" } else { "odd" };

6. Printing: println!

The ! means it's a macro, not a function. {} is a placeholder.

let name = "Alloy";
println!("Hello, {}!", name);          // Hello, Alloy!
println!("{} + {} = {}", 1, 2, 1 + 2); // 1 + 2 = 3

7. Collections: Vec

A growable array. You'll see it everywhere in EVM code — stacks, transaction lists, byte buffers.

let mut v: Vec<i64> = Vec::new();
v.push(10);
v.push(20);
let last = v.pop();   // Some(20)
println!("{:?}", v);  // [10]

{:?} is the debug placeholder — handy for printing structures while you're learning.

8. That's the minimum

You now have enough Rust to read the first chunks of Alloy code. The next lesson exercises this directly.

SyntaxOne-liner
let x = ...immutable variable
let mut x = ...mutable variable
fn name(arg: T) -> R {}function
x.method()method call
if .. else ..branching (also an expression)
println!("{}", x)print
Vec<T>growable list

Don't try to memorize. Recognize it when you see it — that's the goal here.

Summary (3 lines)

  • Variables + functions + ownership + Result + ? + if expression + match + struct/enum + trait + async + macros — the minimum set.
  • "Enough to read 80 % of the source." Not exhaustive; doc.rust-lang.org has the rest when needed.
  • Next lesson: the first homework — write a 0x checker.