skip to content

As maintainer of a widely used Rust checksum or parsing crate, how do you decide whether to support no_std and alloc-only users, and what does that commitment cost?

level: principalimportance: should knowfreq 16%

answer

  1. is the core pure computation
  2. three tiers: core, alloc, std
  3. every dependency must follow
  4. a CI build for a bare target
  5. dropping it later breaks users

basics

~20 s

Support no_std when the crate is pure computation over borrowed data and firmware users depend on it, directly or through other crates. The cost is permanent: std and alloc feature tiers, no_std-capable dependencies, a heap-free core API and CI builds for a target without std.

solid answer

~50 s

I start from what the crate does. A checksum, parser, codec or data structure is pure computation over borrowed bytes, so a core tier costs little; a crate whose job is files, sockets or threads should say plainly that it needs `std`. Then demand: direct firmware users, and foundational position, since a crate that requires mine cannot be `no_std` unless mine is. If I commit, I design in tiers: a `core` API on slices and caller-supplied buffers, `alloc` conveniences returning `Vec` or `String`, and `std` adapters for `io::Read`. The recurring costs are a CI build of each tier for a target such as thumbv7em-none-eabihf, rejecting dependencies that are not `no_std`-capable, error types on `core::error::Error` (MSRV 1.81), and doc notes saying which feature each item needs. Support is a compatibility promise: removing it later breaks firmware users, so an untested half-promise is worse than a clear no.

code

console · 10 lines
console
$ cargo test -q
running 1 test
.
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
$ cargo build --target thumbv7em-none-eabihf --no-default-features
   Compiling crc16 v0.1.0 (/work/crc16)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.06s
$ cargo build --target thumbv7em-none-eabihf --no-default-features --features alloc
   Compiling crc16 v0.1.0 (/work/crc16)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.06s

go deeper

for a junior

Recall that a library can offer core, alloc and std tiers through Cargo features, with std as the default.

for a middle

Explain how the core tier shapes the API: borrowed input, caller-supplied buffers, core::error::Error and no HashMap or float math.

for a senior

Show how the promise is enforced: CI builds of each tier for a bare target, dependency review, and documentation of which feature each item needs.

for a principal

Weigh demand and the crate's position in the graph against permanent maintenance cost, and choose between full support, core only, a split crate or a clear no.

## Start from what the crate is The question is not "can this compile without `std`?" but "is the crate's job independent of an operating system?" - **Natural fits:** checksums, hashes, parsers, encoders and decoders, fixed-point math, protocol state machines, data structures. They take bytes and return values; `core` already has everything they need. - **Poor fits:** crates whose purpose is files, sockets, processes, threads or wall-clock time. A `no_std` mode there either stubs out the point of the crate or grows a portability layer nobody asked for. - **In between:** crates that mostly compute but reach for `HashMap`, float methods such as `f64::sqrt`, or `std::io` traits. Each of those is a design decision, not a mechanical port. ## Weigh demand and position - **Direct users:** firmware, bootloader, kernel and some WebAssembly users file issues asking for it; a few issues from real projects outweigh speculation. - **Transitive position:** a foundational crate decides for everything above it. If a widely used parser is std-only, every driver or protocol crate that requires it is std-only too. Low-level crates carry more responsibility here than applications. - **Maintainer capacity:** a promise needs someone who will keep the extra CI green and review every dependency change against it. ## Design the API in tiers | tier | feature set | what the API can offer | |---|---|---| | core | `--no-default-features` | functions over `&[u8]` and caller-supplied `&mut [u8]`, iterators, fixed-size outputs, error enums implementing `core::error::Error` | | alloc | `--features alloc` | conveniences returning `Vec`, `String` or `Box`, such as "parse into an owned frame" | | std | default `std` | adapters for `std::io::Read` and `Write`, `HashMap`-based indexes, anything touching the OS | The core tier shapes everything above it. Designing around borrowed input and caller-provided output buffers is often a better API anyway, and the `alloc` and `std` layers become thin wrappers. ## What the commitment costs, forever 1. **CI:** a build of each tier for a target without `std`, because host builds and `cargo test` pass even when a dependency links `std`. 2. **Dependency discipline:** every new dependency must itself be usable without `std`, taken with its default features off and its `std` feature forwarded from yours. Some good crates are ruled out. 3. **API constraints:** no `Box<dyn Error>` in the core tier, no `HashMap` without `std`, no `f64::sqrt` without a software implementation. Implementing `core::error::Error` means requiring Rust 1.81 or later. 4. **Feature hygiene:** `std` must be an additive feature that adds API, never a `no_std` feature that removes it, because Cargo unifies features across the graph. 5. **Documentation:** each item that needs `alloc` or `std` says so, or firmware users discover it at link time. 6. **Compatibility:** once firmware users build with default features off, moving an item from the core tier into `alloc`, or dropping `no_std` support, breaks them; the API-evolution material treats that as a breaking change. 7. **Panic discipline:** on firmware every panic ends in the binary's handler, often a reset, so a core tier that returns errors for bad input instead of panicking is part of the promise. ## Questions to answer before committing - Who asked, and which targets do they build for? - Which tier do they need: core only, or `alloc` as well? - Which current dependencies would have to change, and will their maintainers accept that? - What minimum Rust version does the plan imply, such as 1.81 for `core::error::Error`? - Who keeps the bare-target CI job green as dependencies move? ## Making the call - **Commit fully** when the crate is pure computation and either has firmware users or sits low in the dependency graph. Put the tiers in the README and the CI. - **Commit to core only** when owned outputs would drag in too much: keep the promise small and testable. - **Split the crate** (a `no_std` core crate plus a `std` wrapper) when the `std` side is large or pulls heavy dependencies; separate crates make the boundary impossible to break by accident, at the price of two releases to coordinate. - **Decline clearly** when the crate is about OS facilities. A README line saying it needs `std` costs users less than a feature that compiles today and rots tomorrow. What a lead should never accept is the middle: a `no_std` mode nobody builds for a bare target, discovered broken by the first firmware user.

  • When is splitting a Rust library into a no_std core crate and a std wrapper crate better than a `std` feature?
    When the `std` side is large or brings heavy dependencies, or when users keep enabling `std` by accident through feature unification. A separate core crate cannot reach `std` at all, so the boundary holds by construction. The price is two crates to version and release together, and users choosing between two names.
  • What makes dropping no_std support from a published Rust library a breaking change?
    Firmware users build with default features off for targets without `std`. If a release makes the core tier need `std`, or moves an item behind `alloc`, their build fails with E0463 or a missing item after an ordinary `cargo update`. That is breakage in a compatible-looking version, so it needs a major version and a clear note.

  • Adding no_std support with no CI build for a target without std.
  • Treating no_std as free because the crate compiles with the attribute today.
  • Making the core tier depend on a crate that is std-only.
  • Offering a `no_std` feature that removes std instead of an additive `std` one.
  • Promising no_std support for a crate whose purpose is file or network I/O.

Assumes Rust 1.99; implementing core::error::Error needs Rust 1.81 or later, which sets the crate's minimum supported version.