```#![no_std]``` for ordinary apps
⚓ Rust 📅 2025-09-29 👤 surdeus 👁️ 7Hello everyone!
Recently, I was experimenting with creating a CLI #![no_std] programs (built a hello-world program in no-std mode). Though no-std mode is usually set for cases where std is not available, such as operating system kernel, an ordinary app may choose to opt out of using std as well. What I've noticed:
- Since there is no stdto handle start-up anymore, an app has to set#![no_main]and manually define amain()function, like:
fn main(argc: core::ffi::c_int, argv: *mut *mut core::ffi::c_char) -> core::ffi::c_int
Windows apps may have to use fn wmain() with wchar_t argument
- An app may have to disable unwinding panics with panic=abortoption for profiles and building with-Zbuild-std. Also, a panic handler must be defined
- #![no_std]also disables- alloc, but it can be re-enabled with- extern crate alloc;. As far as I understand,- allocis merely a high-level interface to the memory allocator, it doesn't allocate memory itself (and if a crate uses it without- std, it has to define a global allocator)
- libc is not automatically linked, and the crate may fail to build due to missing symbols on which coredepends -- it can be solved by addingcompiler_builtinscrate
I wonder, is there any benefit for apps that can use std, opt out of it? As far as I understand, there may be several benefits:
- More control over start-up and shut-down process of an application, as well as environment and command-line argument processing
- More control over memory allocation
- Being able to opt-out of infallible memory allocation completely (however, as far as I understand, it can require either replacing allocwith your own interface and, as a result, losing the ability to use crates that depend onalloc, or enablingno_global_oom_handlingoption)
- Smaller binary size (I've built two "hello, world" programs -- one with stdand one without it, the ordinary one in--releasemode resulted in a 437 kilobytes binary while no-std one resulted in a 8 kilobytes binary)
2 posts - 2 participants
🏷️ Rust_feed