THE RUSTY TRINITY
Okay Michael, tell us about Aliasing XOR mutability, ownership models, and er, that other thing you said. “Aliasing XOR mutability means that a value may either be lent with multiple read-only references, or lent with a single mutable reference,” explains Michael. “It also means that you can’t pass ownership of a value while it’s being actively referenced. This is technically a best practice to follow regardless of what programming language that you use, but it wasn’t possible to verify at compile time until Rust.
“The ownership model in Rust simply declares that any variable passed by value into a function is transferring ownership of the value to that function. This means that you can’t use a variable again after it’s been passed into a function. This is very useful for API authors because they can ensure that a value that should be used once is consumed on use. Type markers are another interesting compile-time mechanism to prevent misuse of values in APIs. The compiler uses these to prevent misuse of values being moved or shared across threads. This makes developing multi-threaded applications much easier.
“The thing that I particularly like about these features is that they’re what makes the self-documenting nature of Rust code possible. You’ll always know when a function is expected to modify a reference, and ownership transfers make it possible to have compile-time state machines and one-use tokens. They also raise the minimum bar of code quality so that these best practices are adhered by all Rust code. And they make some interesting mechanisms possible, such as compile-time reference counters and the GhostCell research paper (see https://plv.mpi-sws.org/rustbelt/ ghostcell/paper.pdf)”.