Member-only story
Building a Trading Engine in Rust: Part 1
Rust has become increasingly popular for building high-performance systems where reliability and safety are critical. We’ll start building a trading engine from scratch using Rust. This project is perfect for beginners looking to learn Rust through practical application while creating something substantial.
A trading engine, or matching engine, is the core component of any financial exchange. It processes incoming orders and matches them with existing orders in the order book.
Setting Up the Project Structure
We’ll begin by creating the basic structures needed for our trading engine:
Order
- Represents a buy (bid) or sell (ask) orderLimit
- Holds orders at a specific price levelOrderBook
- Contains collections of bids and asks
Building the Order Structure
First, let’s create an Order
struct that will store information about individual orders:
#[derive(Debug)]
enum BidOrAsk {
Bid,
Ask,
}
#[derive(Debug)]
struct Order {
size: f64,
bid_or_ask: BidOrAsk,
}
impl Order {
fn new(bid_or_ask: BidOrAsk, size: f64) -> Self {
Order {
bid_or_ask…