littlefs-rust
Safe Rust API for the LittleFS embedded filesystem.
Built on littlefs-rust-core, a function-by-function Rust port of the C littlefs. No C toolchain required — pure Rust, builds on any target.
On-disk format is compatible with upstream LittleFS for interoperability.
Quick start
use ;
let mut storage = new;
let config = new;
format.unwrap;
let fs = mount.unwrap;
fs.write_file.unwrap;
let data = fs.read_to_vec.unwrap;
assert_eq!;
fs.unmount.unwrap;
Examples
ram_hello.rs — write and read a file
Formats a RAM-backed filesystem, writes a string to /hello.txt, reads it back, and prints storage
statistics on unmount.
ram_tree.rs — directories, rename, remove, and stat
Creates a nested directory tree, lists entries with type and size, renames a file, removes a subdirectory, and queries filesystem usage.
Usage
Implement the Storage trait for your block device:
use ;
Then format, mount, and use the filesystem:
use ;
let config = new; // 4KB blocks, 256 blocks = 1MB
format?;
let fs = mount?;
// Convenience methods
fs.write_file?;
let data = fs.read_to_vec?;
// Directory operations
fs.mkdir?;
for entry in fs.list_dir?
// Fine-grained file access
let file = fs.open?;
file.write?;
file.close?;
fs.unmount?;
Architecture
This crate is a safe wrapper around littlefs-rust-core, which is a faithful function-by-function
translation of the C littlefs. The wrapper handles all the unsafe ceremony: raw pointers,
MaybeUninit, integer error codes, null-terminated paths, and unsafe extern "C" callbacks.
Interior mutability and multiple open files
Filesystem wraps its internal state in a RefCell. Each operation borrows the RefCell only for
the duration of one core call, then releases it immediately. This means multiple File and ReadDir
handles can coexist, and you can interleave file operations with directory iteration:
let mut dir = fs.read_dir?;
while let Some = dir.next
RAII close
File and ReadDir implement Drop, which closes the underlying handle automatically.
Use explicit close() when you need to handle errors from the close operation.
Heap allocation
The crate requires alloc. Internal buffers (caches, lookahead) and file/directory allocations are
heap-allocated via Box and Vec. This provides stable addresses for the core's internal pointer
structures without requiring Pin or self-referential struct tricks.
Feature flags
| Feature | Default | Description |
|---|---|---|
alloc |
yes | Required. Enables Vec, Box, and convenience methods. |
std |
no | Enables std::error::Error impl on Error. |
log |
no | Passes through to littlefs-rust-core for trace logging. |
Prior art
- littlefs2 — idiomatic Rust API wrapping the C library via FFI (requires C toolchain)
- littlefs2-sys — low-level C bindings
License
BSD-3-Clause (same as upstream littlefs)