First of all, the only unstable feature really required to build a no_std
application is the panic_fmt lang item. See reworked no-stdlib example from
the book:
// profile.dev.panic = "abort"
#![feature(lang_items)]
#![no_main]
#![no_std]
// default-features = false, version = "0.2.22"
extern crate libc;
#[no_mangle]
pub extern "C" fn main(argc: isize, argv: *const *const u8) -> isize {
static HELLO: &[u8] = b"Hello, world!\0";
unsafe {
libc::puts(HELLO.as_ptr() as *const i8);
}
0
}
#[lang = "panic_fmt"]
fn panic_fmt(_msg: core::fmt::Arguments, _file: &'static str, _line: u32) -> ! {
unsafe {
libc::exit(1);
}
}
Second, the no-stdlib example in the book is not a good representation of
no_std applications, IME. It says that no_std is meant to be used on systems
where âthreads, networking, heap allocation, and othersâ may not be available,
but then tells you to use the libc crate / C library, which is a library
mainly used to interface the OS to get access to threads, networking, heap
allocation and others. A usual embedded / bare metal Rust application doesnât
link to the C library; of course, you are free to do so if you want to provide
some sort of POSIX layer or etc.
Finally, on some embedded / bare metal applications the panic_fmt
requirement is totally artificial. Itâs usually the case that youâll build your
program so that it never panics (as panics on a system where thereâs no panic
recovery can be as bad as memory corruption) so your final binary, when
compiled in release mode, is free from calls to rust_begin_unwind (what the
panic_fmt lang item maps to). I personally would like to see official support
for this scenario: maybe some -C panic=undefined mode where the compiler does
not demand the panic_fmt lang item and instead just leaves the
rust_begin_unwind symbol undefined; the linker would then either complain at
link time if your program does contain panics or your link program correctly if
it doesnât have any. This undefined mode seems even easier to stabilize than
the panic_fmt lang item as we donât have to decide on any function signature.