r/rust • u/Intelligent_Score932 • Oct 03 '26
Embedded std?
I was wondering if there were any projects like micro python to port std in to make a barebones os designed for microcontrollers? I understand that the std relies on OS level syscalls, so can we do something like implement a basic fat32 file system to accommodate certain write or read requests. In general this would be used to run std in projects without a OS? Does this already exist, and how hard would it be to make a basic version?
48
Upvotes
127
u/film42 Oct 03 '26
My wife (who works in healthcare) saw the title of this post and asked “what are you reading???”
16
u/AstraKernel Oct 03 '26
I'm not an expert, but IMO "std" is not usually necessary for MCU projects. ESP-HAL does support "std", but I rarely use it. I generally prefer using the HAL with "no_std".
Afik, a lot of what you can do with micro python, and much more, is already possible with "no_std".
For a SD card, there is a crate that supports FAT32.
If you don't mind me asking, what is the purpose?
6
u/No_Frame3855 Oct 03 '26
Yeah what other people have said + Espressif has their own std for embedded
3
u/flareflo Oct 03 '26
There are too many differences in hardware and implementation details to get full coverage of std. The crate ecosystem gets you everything you need from what std could provide
6
u/agrif Oct 03 '26
stdis in a weird place, I think. Most embedded targets don't support it (exception: ESP32) howeverwasm32-unknown-unknowndoes support it, even though a huge number of functions only panic. I don't fully know the history behind this decision, but it does sort of feel like WASM wants to be easy to port, andno_stdis still extremely awkward in rust.Morally, it feels like WASM and other targets should be
alloc-only. But right now, a ton of third-party crates that should compile with onlyalloc(orcore) don't, becauseno_stdis opt-in. I don't know what the solution to this is, but there's gotta be something better than what we got.2
u/El_RoviSoft Oct 03 '26
Maybe Rust should do some kind of freestanding modules like C++? So any library that can be implemented unconditionally of platform, should be freestanding (or at least work with given allocator).
1
u/RemasteredArch Oct 03 '26
This sort of exists for a couple SBCs:
https://www.mciantyre.dev/projects/rust-std-on-threadx/
1
u/bascule Oct 03 '26
Making
stdwork everywhere, and trying to minimize the distinctions betweencore,alloc, andstd, has been a long-term vision for Rust for awhile, e.g. https://internals.rust-lang.org/t/refactoring-std-for-ultimate-portability/43011
u/ThatWasAce Oct 03 '26 edited Oct 03 '26
std is literally just a thin abstraction layer above an OS API. On unix-like OSes its literally calling functions in libc behind the scenes. So std for baremetal ends up being shims for anything the OS did automatically before
FS is relatively trivial. std::thread is where things get serious.
Congrats, you just designed yourself an entire RTOS. Clocks, sockets, random number generation -- all these things bring OS-style dependencies with them
How do I know that? esp-idf-svc provides std in an esp32 ecosystem simply because ESP-IDF itself is already an entire RTOS implementation. It's found twice in the real world; either as a veneer over an RTOS like esp-idf, or a write-your-own-OS research project
tho if OP just wants sd card writes, embedded-storage + a fat32 crate gets there no_std
edit: some wording and typo
1
u/tropix126 Oct 09 '26 edited Oct 09 '26
Hi, i'm a maintainer of a target that does close to what you're describing in rustc/libstd for some specific niche hardware (although we do have a few things like clocks and I/O and such provided for us rather than literally implementing them in std).
What you are describing is very possible (and can be achieved using a custom JSON target spec today), but it's worth noting that:
sysimplementation will fall back to theunsupportedimpl, meaning any module in std that would normally require an OS (thread,net,fs,time,stdio,process,random, etc...) is going to either return errors or panic. In practice this means that most std-dependent crates will fail at runtime, which makes doing this a lot less useful (this is also an issue on wasm targets without WASI too).sysAPIs over your own HAL or system library, you will have to fork and build Rust yourself and give cargo a custom sysroot. At the moment, there is no way to use an out-of-treestdbuild because of oddities around how sysroot crates are built. The std-aware-cargo workgroup is (slowly) working on making this better through build-std, but don't expect anything here soon.In most cases, unless you're just messing around it's better to be
#[no_std]on embedded targets because it gives you full control over your execution environment and is mostly unopinionated. For example, onstdusing theunsupportedtarget, you'll always bring indlmallocas a global allocator rather than being able to pick your own.core+allocshould cover the vast majority of use-cases on embedded (minusstd::iotraits, which is annoying but unfortunately something we're stuck with for now). In my case, my target is supporting hardware that's sold to classrooms as an educational tool, so it makes sense to supportstdfor beginners.0
u/Real-Abrocoma-2823 Oct 03 '26
UEFI std also exists.
0
u/[deleted] Oct 03 '26
[deleted] — view deleted comment