Skip to content

Rust

Follow a tick buffer through moves and a shared borrow, then see what Rust’s memory checks cover in a market data pipeline.

Rust is a compiled systems programming language that uses memory ownership and borrowing to manage memory without tracing garbage collection. In a market data pipeline, one stage can hand off a tick buffer and another can inspect it without copying the ticks. The compiler checks which stage still owns the buffer and which references can still be used. The Rust Book, “What Is Ownership?”

Follow one tick buffer

Suppose a feed decoder A fills a heap-backed vector of ticks. The vector holds a pointer to the allocation, the number of ticks it contains and its capacity. A hands the vector to processing stage B by value. This moves the vector: B takes over the existing allocation and A loses access through its original binding. The move transfers the vector’s ownership without duplicating the tick records. Rust standard library, “Vec Guarantees”
B lets a validation routine C inspect the ticks through a shared borrow. C receives a slice, a view of contiguous elements in the vector. B still owns the vector and may read it. Once C has finished with the slice, B can move the vector to D, the next processing stage. If D keeps it until normal scope exit, Rust drops the vector and releases its allocation. The Rust Book, “The Slice Type”
One tick buffer starts with A, moves to B and remains owned by B while C reads a shared slice. After C finishes, B moves the buffer to D.
One tick buffer starts with A, moves to B and remains owned by B while C reads a shared slice. After C finishes, B moves the buffer to D.
The solid band tracks the owner of the same tick buffer. The dashed line shows when C has a borrowed view.

Why the order matters

Move the B-to-D handoff before C’s final read and the compiler rejects it. The borrowed view must stay valid through that read. For the same reason, B cannot append to the vector while C still needs its slice. An append could grow the vector’s allocation, leaving a view into the old storage. The compiler rejects the conflicting access even when this particular append would fit in the existing capacity. Rust compiler error E0505 · The Rust Book, “References and Borrowing”
For an ordinary buffer like this one, several shared references can read the same region at once. An exclusive mutable borrow allows one routine to change that region while other overlapping access is suspended. The compiler’s borrow checker uses lifetimes to establish where references are valid and whether those accesses overlap. In this trace, C’s last use can end the borrow before the enclosing scope ends. The Rust Book, “References and Borrowing”

What the compiler can check

Safe Rust prevents dangling references and data races when the underlying libraries uphold Rust’s safety rules. A decoder that uses a foreign library may cross an unsafe boundary, for example to dereference a raw pointer. The programmer must establish that the referenced storage is valid and stays live for the access, among other requirements. The compiler still checks ordinary Rust borrows inside an unsafe block. The Rust Book, “Unsafe Rust” · The Rust Reference, “Behavior considered undefined”
Moving this vector avoids copying its payload. End-to-end latency also includes work such as decoding, queueing and allocating when buffers grow. Measure the full path under the message load it needs to handle. Rust standard library, “Capacity and reallocation”
The vector can safely hold a stale quote or a batch that skips an exchange sequence number. The feed handler still has to detect those conditions before the trading logic uses the data. The SelfMadeQuant lesson works through how sequence numbers and receipt times determine the market state a strategy can use. It has a public preview, with the full lesson available to members. Trades, Quotes and Market State

Sources

Last updated
  • rust
  • systems programming
  • memory ownership
  • market data
  • latency
← Back to the glossaryBack to top ↑