Sharing references between Python and Rust

EDIT: I’ve revised this article so that people reading it later do not reproduce the bug that was originally in it. This experiment has been upstreamed to the cpython crate and can now be used by everyone.

In 2018, the Mercurial project decided to use Rust to improve performance and maintainability of previous high-performance code, you can read more about it in the Oxidation Plan.

While one may argue that Rust took inspiration from Python in some aspects of its semantics, the two languages don’t share a lot of similarities at a lower level. Rust strict memory borrowing rules and default immutability don’t play very nice with some of Python’s features: its dynamic typing, mutability rules, classes, garbage collecting, to name the big ones.

[Read more]