Rust Reference ยท Attributes
Crate-level attributes: no_std, no_main and feature
Reference for Rust crate-level attributes: no_std, no_main, feature gates on nightly, recursion_limit, crate_type and forbid(unsafe_code), with edge cases.
An inner attribute #![...] written at the very top of a crate root (main.rs or lib.rs) configures the entire crate. A few of them change what the crate is allowed to use or how it starts, and they are central to embedded, kernel and WebAssembly work.
#![no_std] drops the standard library, leaving only core (and optionally alloc). #![no_main] tells the compiler not to generate the usual main entry point, because you supply your own start symbol. #![feature(name)] turns on an unstable language or library feature and works only on the nightly compiler.
#![no_std] #![no_main]
Try it
A real no_std binary needs a target without an operating system, a panic handler and its own entry point, none of which fit the single-file runner. These examples use the pieces that do work: no_std plus an explicit extern crate std, and attributes that tune the compiler.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
#![no_std] #![no_main] #![feature(name1, name2)] // nightly only #![recursion_limit = "256"256"] #![crate_type = "lib"] #![forbid(unsafe_code)]
| Attribute | Purpose |
|---|---|
#![no_std] |
Link core instead of std; no heap, files, threads or println! unless you add them back |
#![no_main] |
Skip the compiler-generated main wrapper; you export main or _start yourself, often with #[unsafe(no_mangle)] |
#![feature(x)] |
Enable unstable feature x; error on stable and beta |
#![recursion_limit = "N"] |
Maximum macro expansion and trait resolution depth (default 128) |
#![forbid(unsafe_code)] |
Reject any unsafe in the crate |
#![windows_subsystem = "windows"] |
On Windows, build a GUI app without a console |
Errors and edge cases:
- A
no_stdbinary must define a panic handler with#[panic_handler], and usually setspanic = "abort"in the profile. #![feature]on stable gives error E0554 (#![feature] may not be used on the stable release channel).no_stdis about linking, not about the language:core::paths, slices,Optionand iterators all still work.
Usage notes
- Libraries can be conditionally no_std. The common pattern is
#![cfg_attr(not(feature = "std"), no_std)], so a crate works in both environments. - Memory.
extern crate alloc;bringsVec,StringandBoxback, provided a#[global_allocator]exists. - Prelude differs. Under
no_std, the prelude iscore::prelude, so some names need explicituse core::...imports. - Nightly features change without notice. Pin a toolchain with
rust-toolchain.tomlif you must use one. no_mainpairs with C-style startup, as in embedded runtimes (cortex-m-rt) and some WebAssembly setups.- Placement. Inner attributes must come before any items in the file, after only comments and other inner attributes.