The Rust programming language celebrated its 5th-anniversary last month. Rust will continue growing into a cross-platform and full-stack programming language of choice for the next decade. I’m into this journey almost halfway through, and since then, I have had the chance to work on several Rust projects and witness different improvements in the language and Rust libraries.

However, to celebrate the five years, many blog posts have been written explaining why Rust is a language of choice and where to locate relevant resources to use Rust across sites and along with the software stack. In this particular post, we’ll go through the different programming languages that are binding with Rust.

Bindings with other programming languages

Some of the target systems mentioned earlier are made possible by the ability of Rust to interact with other programming languages. For illustration, Android apps are fundamentally written in Java, so your Rust code needs to interact with Java to compute it in an Android application.

The first language is C, with which Rust can interact utilizing the so-called foreign function interface (a.k.a. FFI). You can find more documentation in the Rust notion, but you can compile a Rust library and expose it as a C library. And vice-versa, inside your Rust code, you can call functions from a C library. it is done through simple annotations like extern “C” or # [no_mangle] and support libraries like the std::ffi module and labs crate.

Once Rust interacts with C, it can integrate with any programming language that interfaces with C, including several languages. Examples are:

·  Java. The JNI crate, mentioned above for Android, enables interaction with Java programs through the Java Native platform (written initially to interact with C).

·  Python. The PyO3 crate offers binding with the Python interpreter.

·  C++. It’s entirely complex to map all of the concepts of the C++ language to Rust. Besides Bingen and cbindgen, which usually interact with the C subset of C++, we can mention the cxx crate, which attempts to map more complex C++ classes like std::string or std::vector directly to Rust. I also recommend watching An unholy fusion of Rust and C++ in PhysX-rs, a talk about glueing a sizeable C++ SDK into a Rust crate. Another piece of equipment that was just released is the safer_ffi project.

·  Other languages. More bindings are available, like the Ruby, V8 JavaScript interpreter, OCaml, R, etc.

Other targets

Rust’s breadth of targets is limitless, and the following exciting targets were found while doing more research for the blog post (disclaimer: it has not been tested).

·  BPF code written in Rust. BPF is a virtual machine operating inside the Linux kernel, enabling it to run user-defined programs to monitor activities in the kernel. It’s essential for applications like implementing load balancing, monitoring performance, mitigating DDoS, etc. And you can also write such programs in Rust!

·  General Purpose GPU (GPGPU). As an illustration, the accel crate offers a GPGPU computing site based on CUDA. You can write programs with it in Rust and operate them on a GPU that supports CUDA.

Remarks on cross-compilation

these are a few elements that make Rust shine as a cross-platform language to collect the section sites by Rust.

First, Rust covers numerous platform-specific elements behind tangible abstractions in the suitable library. For illustration, the u32::trailing_zeros function counts the number of zero bits at the end of a 32-bit integer, looks pretty sure. One could use that as a loop to check and measure the bits, but it turns out that most CPUs have a specific instruction to do this operation faster than the usual “naive” loop. So Rust makes it available behind trailing zeros abstraction, dispatches either a CPU-specific instruction or more instructions. In C/C++, you would have to support it yourself and either have huge custom #ifdef in your code to check for several compilers and target systems, or give up and use the slow naive loop.

As another illustration, the std::fs::Metadata type represents file system metadata concepts on all the supported operating systems, like the creation or modification time or whether the path represents a file or a directory. Using it in your code is completely transparent, regardless of the target (Linux, Windows, or OSX). Then, you have the access to OS-specific elements in std::os::linux::fs::MetadataExt std::os::unix::fs::MetadataExt or std::os::windows::fs::MetadataExt. So Rust effortlessly supports platform-specific and also cross-platform APIs.

You don’t have to check for specific multiple compilers as you would in C++ to use non-standard functions of some compilers or JavaScript to support older or exotic web browsers, for there is a single rust compiler.

Last, if you want to find out even more about cross-compilation in Rust, you should read all you need to know about cross-compiling Rust programs by Jorge Aparicio.

Conclusion

We hope this blog post convinced you that Rust is a programming language of choice if you’re targeting several platforms, whether on mobile, desktop, embedded systems, web, or even more exotic “sites” like writing BPF code for the Linux kernel.