Languages & RTOS

LittleFS Rust: Embedded Storage with the littlefs2 Crate

LittleFS reaches Rust through the littlefs2 crate, with an API modelled on std::fs. Here is how the Storage trait and mounting work.

Illustration of a gear-shaped crate connecting to flash memory blocks

LittleFS has a mature Rust wrapper in the littlefs2 crate, which brings the C filesystem to embedded Rust with an interface modelled on the standard library's std::fs. For Rust developers working on microcontrollers, it offers a proven on-flash format with the ergonomics of Rust types and error handling. This article explains how the crate is structured, how to connect it to your flash, and the two build gotchas that catch most people on no_std targets.

LittleFS details in this article come from the upstream littlefs repository and the littlefs2 documentation on docs.rs, checked on 23 November 2026, when the crate's latest version was 0.8.1. Rust crates evolve quickly, so consult the docs for the version in your Cargo.lock before copying exact signatures.

What littlefs2 is

LittleFS is written in C, and littlefs2 builds a safe Rust API on top of the littlefs2-sys bindings, which compile the C sources for your target. The upstream README lists it among related projects as a Rust wrapper that lets you use littlefs with a Rust-friendly API while benefiting from Rust's memory safety. The crate is dual-licensed under Apache-2.0 or MIT, while the underlying littlefs code remains BSD-3-Clause.

LittleFS through littlefs2 keeps the on-flash format identical to the C library. Images created by C firmware, by littlefs-python on a PC or by Rust code are therefore interchangeable, provided the geometry matches.

The Storage trait

LittleFS needs a block device, and in littlefs2 you supply it by implementing the littlefs2::driver::Storage trait. According to the documentation, the trait defines the block device through constants, described as both actual and typenum constants, and requires methods to read, write and erase. The constants correspond to the familiar C configuration fields: read size, write (program) size, block size, block count, cache size, lookahead size and block cycles.

LittleFS geometry rules are the same in Rust as in C. The block size must be a multiple of the read and write sizes, and the cache size must be a multiple of both and a factor of the block size. Getting these wrong produces errors at mount time, so derive them from your flash datasheet.

Why typenum?

LittleFS buffers in littlefs2 are sized at compile time. Using typenum constants lets the crate declare arrays whose length depends on your storage configuration without requiring heap allocation, which is valuable on microcontrollers that have no allocator at all.

Formatting and mounting

LittleFS usage in littlefs2 follows a clear sequence, illustrated in the crate documentation with a RAM-backed example created by the ram_storage! macro:

  1. Create a storage backend that implements Storage.
  2. Format it with Filesystem::format before the first mount.
  3. Allocate the filesystem state with Filesystem::allocate, typically in static memory.
  4. Mount with Filesystem::mount, passing the allocation and the storage.

LittleFS operations then follow the std::fs style. The documentation's full example opens a file with options, writes to it, seeks and reads the data back. Because operations return Result, failures such as a missing file or a full filesystem are handled with ordinary Rust error handling instead of checking negative integers.

// Outline only: check the docs for your crate version
let mut storage = MyFlash::new(/* peripherals */);
Filesystem::format(&mut storage).unwrap();          // first boot only
let mut alloc = Filesystem::allocate();
let fs = Filesystem::mount(&mut alloc, &mut storage).unwrap();
// use fs to create, read and write files

LittleFS mounting in production should mirror the C pattern: try to mount, and only format if mounting fails, so that existing data is never erased by mistake.

The two no_std gotchas

LittleFS in littlefs2 is no_std compatible, but the documentation highlights two pitfalls.

Gotcha 1: the dependency resolver

LittleFS bindings in littlefs2-sys have a development dependency, memchr, that enables its std feature. With the old Cargo resolver, that feature can leak into your firmware build and fail to compile for a bare-metal target. The fix is to use Rust 1.51 or later and set resolver = "2" in your package. The docs note this is the default in the Rust 2021 edition, so new projects are usually fine.

[package]
name = "my-firmware"
edition = "2021"     # implies resolver = "2"

Gotcha 2: strcpy

LittleFS's C source calls strcpy. If your target does not link a C library that provides it, the link step fails with an undefined symbol. littlefs2 offers a c-stubs feature that supplies an implementation.

[dependencies]
littlefs2 = { version = "0.8", features = ["c-stubs"] }

Implementing Storage for real flash

LittleFS storage implementations in Rust usually wrap an existing flash driver. Many embedded Rust HALs implement the embedded-storage traits, which provide read, write and erase operations. A typical approach is to write a small adapter struct holding the HAL flash peripheral and an offset, then implement Storage by translating block and offset into absolute addresses, just as a C port would.

LittleFS's rules about programming still apply: never write to flash that has not been erased, respect the chip's page boundaries and ensure reads after writes return fresh data. The filesystem relies on these properties for its power-loss guarantees.

Logging with defmt

LittleFS errors are easier to diagnose with logging, and littlefs2 lists defmt as an optional dependency, which suits the widely used defmt logging framework on Cortex-M targets. Enable it if your project already uses defmt for diagnostics.

When to choose littlefs2

LittleFS in Rust makes sense when:

  • your firmware is written in Rust and you want typed, Result-based file operations;
  • you need data compatibility with existing C firmware or PC tools;
  • you want a filesystem with strong power-loss guarantees and bounded memory.

LittleFS in C may still be simpler if your project is mostly C with a thin Rust layer, because you avoid maintaining bindings across a language boundary.

Troubleshooting

Symptom Likely cause Fix
std errors when building for a microcontroller Old resolver pulling memchr with std Use edition 2021 or resolver = "2"
Undefined strcpy at link time No libc on the target Enable the c-stubs feature
Mount error on first boot Storage never formatted Format only when mount fails
Corrupt or inconsistent data Storage implementation writes without erase or crosses pages Fix the adapter to follow flash rules
Compile errors around constants Geometry types not matching Re-check typenum constants and divisibility rules

Designing a Rust storage adapter

LittleFS adapters in Rust benefit from a little up-front design. Decide which flash region belongs to the filesystem and encode its base address and size as constants, so that the adapter can never touch your application code. Make the block count a compile-time constant derived from those values, which allows the typenum-based buffers to be sized correctly.

LittleFS adapters should also be tested on the host before reaching hardware. Because the Storage trait is just a set of methods, you can implement it over a Vec<u8> or a fixed array, run your application's storage logic in ordinary unit tests, and even simulate a power cut by stopping writes part-way through. The crate's own RAM storage macro exists for exactly this kind of experiment.

LittleFS error handling in Rust is clearer than in C because each failure arrives as a typed error rather than a negative integer. Treat "no space" and "corrupt" errors differently: the first is an application decision, such as deleting old logs, while the second usually points to a defect in the storage adapter that needs fixing rather than handling.

Integrating with an async or RTOS environment

LittleFS operations in littlefs2 are synchronous, because the underlying C library performs blocking reads and writes through your storage callbacks. In async Rust firmware, for example with Embassy, the usual approach is to give the filesystem to a single task that owns the flash and to send it requests over a channel. That keeps blocking flash operations contained and avoids sharing the filesystem between tasks, which the C library does not support without explicit locking.

Testing a Rust storage adapter

The storage adapter is the boundary between filesystem operations and the physical device. Begin by documenting the flash region, erase size and alignment requirements of the hardware. The adapter should translate a filesystem request into operations that stay inside the reserved region, and it should return an error when a request cannot be completed. A successful return must mean that the operation actually completed according to the driver's contract.

During development, use a simulated storage implementation to exercise normal reads and writes as well as failure paths. Initialise the simulated region in the same erased state expected by your flash device, then test boundary offsets and the final block. Introduce failures during erase, program and read operations and confirm that the application reports them rather than assuming that data was saved.

On the target board, check the relationship between the driver's buffers and the filesystem's configured sizes. Embedded Rust projects often operate with limited RAM, so measure the combined cost of caches, open files, task stacks and application buffers. A configuration that compiles is not necessarily comfortable under the real workload.

Plan ownership of the mounted filesystem explicitly. If multiple tasks need access, provide a clear synchronisation strategy and avoid keeping a lock while performing unrelated work. Test startup, repeated mounts, full storage and resets during updates. When changing a file format, include a version and validate decoded values before using them. Preserve a backup of important settings during upgrades and make recovery behaviour visible to the user or operator.

Frequently Asked Questions

Is littlefs2 an official littlefs project?

LittleFS's upstream README lists littlefs2 among related projects as a Rust wrapper, but it is maintained separately from the C repository.

Does littlefs2 require an allocator?

LittleFS through littlefs2 is designed for no_std use, with buffers sized at compile time through typenum, so no heap allocator is needed.

Can Rust and C firmware share a LittleFS image?

LittleFS keeps the same on-disk format in both, so images are compatible when the geometry matches.

Which licence does littlefs2 use?

LittleFS bindings in littlefs2 are licensed Apache-2.0 or MIT, while the bundled littlefs C code is BSD-3-Clause.

What is the latest littlefs2 version?

LittleFS's Rust crate was at version 0.8.1 on docs.rs when we last checked; confirm on crates.io before starting a project.

Can littlefs2 be used with Embassy?

LittleFS through littlefs2 can run in an Embassy application, typically inside one task that owns the flash peripheral and serves file requests from others.

Should I store large datasets with littlefs2?

LittleFS handles large files well because file data uses copy-on-write skip-lists, but remember that available capacity is set by your Storage block count. For gigabytes of data, an SD card with a separate block device may be more appropriate than on-chip flash.

Conclusion

LittleFS in Rust gives you the reliability of a widely deployed embedded filesystem with the safety and expressiveness of Rust. Implement the Storage trait carefully, format only when mounting fails, and remember the resolver and c-stubs settings for bare-metal builds. If you are evaluating an RTOS for a Rust or C project, our LittleFS Zephyr article shows a fully integrated alternative.

Keep reading

More LittleFS articles