How to use the std crate on embedded platforms


#1

I’ve recently started to look at Rust since it seems a very promising language to me (I come from C/C++ mainly).
I’ve also done lot of embedded development in the past few years and so I looked around on how to use Rust for that too.
Lots of things are already available but I still can’t find an answer to a question I have: what needs to be done in order to be able to use the std crate?
I completely understand that it can’t be used directly since there is no OS available to handle memory allocation, threads and stuff but how could I make this possible?
When doing C/C++, I’m used to have a file with all the standard system calls (_write(), _sbrk(), _fork(), etc) provided with a either a real implementation (redirecting _write() calls to a serial port for instance) or a dummy implementation that just fails if you try to use it.
Is it possible to have this kind of mechanism? I would love to be able to glue Rust’s std library with some kind of basic OS (FreeRTOS for example) to have an experience as natural as possible.
Thanks


#2

One of the search terms you are looking for is “lang_items” that you can implement, such as the memory allocator and the panic unwinder.

I’m not good at rust embedded, but I’ve learned a thing or two by lurking Phill Opperman rust kernel series.

https://os.phil-opp.com/

Also:

and japaric’s work is excellent:

Especially the “photon quickstart” may be of interest, that glues rust to the freeRTOS-based particle.io runtime, using dark magicks that are currently beyond me:

I haven’t tried things myself (yet)


#3

Thank you very much for the information, I’ll look into it!


#4

Oh, and before I forget!

A lot of things from std, that don’t need allocations, such as the Result and Option types, can be used by selectively useing them from core.

The std “rust preamble” is just a convenience re-export of core and some other useful things


#5

You might also be interested in https://internals.rust-lang.org/t/refactoring-std-for-ultimate-portability/4301 and https://github.com/rust-lang/rfcs/blob/master/text/1868-portability-lint.md


#6

The refactoring of std looks very promising!
The only downside it’s that it will take a still undefined amount of time to be completed so I don’t know if it’s better to wait for it or do something now, knowing that it’s only temporary. It was for a side/hobby project so maybe it’s better to wait and do something else in the meantime.