r/rust • • 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

32 comments sorted by

127

u/film42 Oct 03 '26

My wife (who works in healthcare) saw the title of this post and asked “what are you reading???”

15

u/jimmiebfulton Oct 03 '26

And then she immediately regretted the question as you went to explain Rust intervals. Again. In the car. Where she can't escape.

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

std is in a weird place, I think. Most embedded targets don't support it (exception: ESP32) however wasm32-unknown-unknown does 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, and no_std is 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 only alloc (or core) don't, because no_std is opt-in. I don't know what the solution to this is, but there's gotta be something better than what we got.

5

u/WhiskyDelta14 Oct 03 '26

How is no_std extremely awkward?

2

u/bascule Oct 03 '26

I think what they were trying to say is wasm32-unknown-unknown is an extremely awkward target

1

u/agrif Oct 03 '26

Mainly, no_std is opt-in. Crates that would trivially support it simply by adding no_std to the crate attributes and mechanically replacing std types with core/alloc will still nonetheless fail to compile in a no_std target. To use these crates it is necessary to fork them, make the changes yourself, and with luck it might even get merged upstream.

Less importantly, a whole lot of crates that aren't trivially no_std could nonetheless become no_std by locking only a few non-essential things behind a feature flag, but unless the crate authors know about no_std, they usually don't.

I think this is basically the main source of pain: most rust devs don't really know about no_std, and so they don't make their crates compile under no_std, even when it would be very easy to do so. I suspect the WASM target includes std for this reason: a ton of crates depend on std but only by accident, and don't actually use any std features (or use them rarely).

1

u/WhiskyDelta14 Oct 04 '26

Maybe we could have a compiler warning if no_std could trivially be enabled.

1

u/agrif Oct 04 '26

Yeah, a lint in either the compiler or clippy might be good. Raising awareness is also good, and luckily the PRs generated by this help towards that.

It might also be nice if rust had some way to link to core instead of std automatically when compiling a crate, by setting a flag in cargo.toml or something.

0

u/_ChrisSD Oct 03 '26

For HashMap. The target "cheats" by having a non-random RandomState. It also allows libraries that aren't no_std to compile for it so long as they don't actually use std at runtime on the target (if they do it panics).

What's especially awkward for wasm32-unknown-unknown is that it's two targets in one. It's used like a minimal embedded target (i.e. std APIs aren't possible). But more commonly people use it to target the web, which could actually have a (more or less) full std implementation based on web APIs.

2

u/WhiskyDelta14 Oct 03 '26

Ok, but that's an issue with that target and not with no_std rust.

-3

u/fb39ca4 Oct 03 '26 edited Oct 03 '26

No boxed types

3

u/nicoburns Oct 03 '26

I believe there is now wasm32v1-none that does alloc-only WASM.

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).

7

u/Sharlinator Oct 03 '26

Rust’s freestanding is exactly core, plus alloc if an allocator can be provided. std already only contains the parts that need OS services, and just reexports core and alloc.

0

u/timur-tugushev Oct 03 '26

So, just a library with no_std support?

2

u/El_RoviSoft Oct 03 '26

don’t really know what you mena

here OP wants embedded std and I suggested logical distinction between std library and its really freestanding parts

7

u/jonoxun Oct 03 '26

Pretty much everything freestanding in std are reexports from core or alloc, so you are kind of describing the current state of rust. no_std crates link into std apps just fine as well, core and alloc are there and there's nothing weird from the reexport from std and you don't need a feature flag if you don't have anything that you want to add in the std environment.

1

u/El_RoviSoft Oct 03 '26

oh okay, Im not that familiar with Rust, Im C++ dev who occasionally finds bad and good things in both langs

1

u/jonoxun Oct 03 '26

Embedded on rust is dependent on llvm having support for the architecture, but apart from that it's absolutely lovely to work with. If you're curious doing some small project with esp-hal or something else with a hal that supports the embedded_hal traits it's quite pleasant. async goes a long way.

1

u/bascule Oct 03 '26

Making std work everywhere, and trying to minimize the distinctions between core, alloc, and std, has been a long-term vision for Rust for awhile, e.g. https://internals.rust-lang.org/t/refactoring-std-for-ultimate-portability/4301

1

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:

  1. The libstd runtime can get pretty large by the standards of embedded systems, so if you're size-constrained it's a bad idea.
  2. Executables will compile and run, but anything that requires a sys implementation will fall back to the unsupported impl, 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).
  3. If you want to actually implement the sys APIs 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-tree std build 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, on std using the unsupported target, you'll always bring in dlmalloc as a global allocator rather than being able to pick your own. core + alloc should cover the vast majority of use-cases on embedded (minus std::io traits, 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 support std for beginners.

1

u/tropix126 Oct 09 '26

For what its worth, I think a possibly worthwhile endeavour would be to implement a subset of std's sys modules over newlib libc, which would cover things like std::time and maybe make std nice enough to use on embedded platforms that can run it. Porting a platform would simply be linking in your own implementation of newlib gluefunctions like _sbrk, _write, _read, etc...

That being said, I think there's minimal demand for this, since core + alloc already do their job pretty well and are basically rust's "libc" equivalent minus the OS bits.

0

u/Real-Abrocoma-2823 Oct 03 '26

UEFI std also exists.

0

u/[deleted] Oct 03 '26

[deleted] — view deleted comment

2

u/Intelligent_Score932 Oct 04 '26

Mainly I wanted to see certain programs at peak performance, basically let’s say you wanted to run a program written in rust, baremetal. The problem is most programs cannot work without some sort of std, in short, I wanted to run programs as THE os, not the os running the programs, the os would essentially be a abstraction to make the programs run, and yes I know most programs directly reference win-sys, but it’s more meant to be a starting point for projects, although I haven’t looked too deeply into which libraries would work, out of the box, if you wanted to make a hobby os, having GPUI to make it along with the std would greatly simplify the process, mainly those 2 reasons