getcracked
Blog / Engineering

Why Quants Are Switching to Rust

Engineering

C++ has been the king of HFT for 30 years. But segfaults are expensive. Rust promises C++ speed with Python safety.


The Problem with C++

C++ gives you raw power, but it also gives you a thousand ways to shoot yourself in the foot. Memory leaks, buffer overflows, and race conditions are common. Debugging a production crash in a trading engine that processes millions of orders a second is a nightmare.

The Rust Promise

Zero-Cost Abstractions + Memory Safety

Rust manages memory at compile time using a system called "Ownership and Borrowing". There is no Garbage Collector (like Java/Python) to pause your program randomly. But there are also no manual malloc/free calls to screw up.

Key Features for Trading

  • Performance: Matches C++ in almost every benchmark.
  • Fearless Concurrency: The compiler literally won't let you write data races.
  • Modern Tooling: Cargo (package manager) is lightyears ahead of CMake.

Code Comparison: Order Matching Engine

Here is a simplified example of how clean a Rust matching engine loop can look compared to C++.

1
use std::collections::BinaryHeap;
2
use std::cmp::Ordering;
3

4
#[derive(Eq, PartialEq)]
5
struct Order {
6
    id: u64,
7
    price: u64,
8
    quantity: u64,
9
}
10

11
// Implement ordering so we can use a BinaryHeap (Priority Queue)
12
impl Ord for Order {
13
    fn cmp(&self, other: &Self) -> Ordering {
14
        // Reverse ordering for Min-Heap behavior on Ask side
15
        other.price.cmp(&self.price)
16
    }
17
}
18

19
impl PartialOrd for Order {
20
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
21
        Some(self.cmp(other))
22
    }
23
}
24

25
struct OrderBook {
26
    bids: BinaryHeap<Order>, // Max-Heap by default
27
    asks: BinaryHeap<Order>, // Min-Heap via custom Ord
28
}
29

30
impl OrderBook {
31
    fn match_orders(&mut self) {
32
        while let (Some(bid), Some(ask)) = (self.bids.peek(), self.asks.peek()) {
33
            if bid.price >= ask.price {
34
                // Determine trade quantity
35
                let trade_qty = std::cmp::min(bid.quantity, ask.quantity);
36
                println!("Trade executed: {} @ {}", trade_qty, ask.price);
37
                
38
                // Real implementation would handle updating/popping orders
39
                // This is just to show the clean syntax
40
                break; 
41
            } else {
42
                break;
43
            }
44
        }
45
    }
46
}
47

48
fn main() {
49
    println!("Rust builds safety into the type system.");
50
}

Who is using it?

While C++ is still dominant (legacy codebases are huge), new projects are increasingly being written in Rust.

  • Jump Trading: Heavily invested in Rust for new strategies.
  • Hudson River Trading (HRT): Using Rust for internal tools and data pipelines.
  • Crypto Firms: Almost entirely Rust (Solana, Polkadot, etc.).