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;).returnis allowed but not idiomatic. - Ownership.
let s1 = String::from("hello"); let s2 = s1;moves s1 (no longer usable).&s1borrows.&mut s1borrows mutably. - Result / Option /
?.Result<T, E>(success / failure) +Option<T>(some / none) +?for early-return on error. ifis an expression.let x = if cond { a } else { b };. No ternary operator;ifitself 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 inimpl 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<...>;.awaitto 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
| Type | Meaning |
|---|---|
i32, i64 | signed integers |
u32, u64, u128 | unsigned integers |
bool | true / false |
&str | borrowed string (read-only, lightweight) |
String | owned string (mutable, heap-allocated) |
The
&strvsStringdistinction trips everyone up at first. For now:&stris "looking at someone else's house,"Stringis "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.
| Syntax | One-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) | |
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 +
?+ifexpression +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.