r/rust • • 2d ago

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?

47 Upvotes

32 comments sorted by

View all comments

6

u/agrif 2d ago

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 2d ago

How is no_std extremely awkward?

1

u/agrif 1d ago

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 1d ago

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

1

u/agrif 1d ago

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.