Rust Library Compatibility Levels
From microcontrollers to full-blown Linux systems, Rust has you covered!
2023-09-01
3 minute read
Rust is a general-purpose language, as such it can be used in a variety of environments going from bare-metal microcontrollers(as previously explored here) to Linux systems with many layers of abstractions. As such the libraries developed for it sometimes may or may not work in different environments, depending on what they use.
So let’s go lower in terms of features and abstractions, but higher in terms of compatibility:
- std(PCs, some microcontrollers)
- ‘std’ stands for standard library and is Rusts default library offering many useful data structures and Abstractions going from Strings to TCPStreams
- The default Rust run mode(If you’ve ever written “Hello World” in Rust you used it)
- (WASIX)(Superset of WASI, personally I remain sceptical of this one, as it replicates Unix APIs and it may repeat mistakes of the past)
- More Unix-like APIs which can be built upon, which makes it possible to run cURL for example
- WASI(Extended Webassembly, adding more APIs)
- This enables many System calls, enabling more complex programs to run. wasmtime is a runtime supporting this. These runtimes can function as lightweight container alternatives
- WASM(Webassembly, most commonly seen in Browsers)
- Most commonly used for games in browsers, but it’s a binary format which is very portable, which explains the browser use case
- You can even use network operations!(Threading for example is limited though)
- no-std + alloc(Many Microcontrollers)
- like no-std but with allocations, which helps a lot for e.g. Vectors
- no-std(Runs even on the smallest of microcontrollers, when you’re writing bare-metal code with minimal abstractions)
#[no_std]
ensures no standard library(except for the platform-agnostic core part) is used. Crates have to be marked as#![no_std]
to be used in this mode.- no heap allocations of any kind(like String, Vec etc.)
Different libraries support different levels of compatibility, and often allow running on no_std
via no-default-features, such as serde or serde_json, both of which support no-std with some restrictions.
And it has to be said that no library author is obliged to support any of these levels, but it’s nice if they do, as I found out while playing around with ESP32 microcontrollers. And if you are designing a library, it might be worth considering supporting some of these layers, as it makes your library more versatile and thus more useful, and also mentioning it clearly in the README.
Rust also has a list of targets along with what they support.
And are the levels mentioned in the article in a simple graphic you can download and share(created using the excellent Excalidraw):
I hope this was informative to you. If you have any questions or suggestions feel free to contact me. And if you want to support me and my work, you can do so here.
Thanks to my sponsors flyaruu for making these posts possible!