Embedding Rust
So far, we've seen how to embed C and Lua into Rust. But, what if you want to combine Rust into other programming languages? Doing so is a very handy technique for improving runtime performance in interpreted languages, making memory-safe extensions where once you might have been using C or C++. If your target high-level language has difficulty with concurrency embedding, Rust is a further win. Python programs suffer in this regard—at least those implemented on CPython or PyPy—because of the Global Interpreter Lock, an internal mutex that locks objects' bytecode. Offloading computation of large blocks of data into a Rust + Rayon extension, for example, can be both straightforward to program and improve computation speed.
Well, great. How do we make this sort of thing happen? Rust's approach is simple: if you can embed C, you can embed Rust.
Into C
Let's embed some Rust into C. The quantiles library (https://crates.io/crates/quantiles)—discussed in Chapter 4, Sync and Send – the...