Implementing Littlefs2 filesystem for W25Q external flash
⚓ Rust 📅 2025-07-29 👤 surdeus 👁️ 11Hello guys, Rust noob here, I am currently trying to implement filesystem using littlefs2 crate for W25Q128FVSG external flash. I encounter this error about lifetime and mutable borrow when writing mount function. Basically, it will try to mount first, if it returns an error then the filesystem will be formatted and try to mount again:
pub fn mount(&'a mut self) -> LfsResult<()> {
// Step 1: Attempt mount using temporary variables (avoid tying lifetime to self)
let fs_result = {
let fs_alloc = &mut self.fs_alloc;
Filesystem::mount(fs_alloc, &mut self.storage)
};
if let Ok(fs) = fs_result {
self.fs = Some(fs);
return Ok(());
}
// Step 2: Temporarily replace storage so we can format
let mut old_storage = core::mem::replace(
&mut self.storage,
FlashStorage::new(W25Q128FVSG::new_dummy()),
);
Filesystem::format(&mut old_storage).map_err(|e| {
log::error!("Format failed: {:?}", e);
e
})?;
self.storage = old_storage;
// Step 3: Second mount attempt (again using local lifetimes only)
let fs = {
let fs_alloc = &mut self.fs_alloc;
let storage = &mut self.storage;
Filesystem::mount(fs_alloc, storage)?
};
self.fs = Some(fs);
Ok(())
}
This is the error from the compiler:
error[E0499]: cannot borrow `self.storage` as mutable more than once at a time
--> src\mem\fs.rs:153:13
|
95 | impl<'a> LittleFs<'a> {
| -- lifetime `'a` defined here
...
143 | Filesystem::mount(fs_alloc, &mut self.storage)
| ----------------- first mutable borrow occurs here
...
147 | self.fs = Some(fs);
| ------------------ assignment requires that `self.storage` is borrowed for `'a`
...
153 | &mut self.storage,
| ^^^^^^^^^^^^^^^^^ second mutable borrow occurs here
error[E0506]: cannot assign to `self.storage` because it is borrowed
--> src\mem\fs.rs:162:9
|
95 | impl<'a> LittleFs<'a> {
| -- lifetime `'a` defined here
...
143 | Filesystem::mount(fs_alloc, &mut self.storage)
| ----------------- `self.storage` is borrowed here
...
147 | self.fs = Some(fs);
| ------------------ assignment requires that `self.storage` is borrowed for `'a`
...
162 | self.storage = old_storage;
| ^^^^^^^^^^^^ `self.storage` is assigned to here but it was already borrowed
error[E0499]: cannot borrow `self.fs_alloc` as mutable more than once at a time
--> src\mem\fs.rs:166:28
|
95 | impl<'a> LittleFs<'a> {
| -- lifetime `'a` defined here
...
142 | let fs_alloc = &mut self.fs_alloc;
| ------------------ first mutable borrow occurs here
...
147 | self.fs = Some(fs);
| ------------------ assignment requires that `self.fs_alloc` is borrowed for `'a`
...
166 | let fs_alloc = &mut self.fs_alloc;
| ^^^^^^^^^^^^^^^^^^ second mutable borrow occurs here
Some errors have detailed explanations: E0499, E0506.
For more information about an error, try `rustc --explain E0499`.
warning: `telematics-control-unit` (bin "telematics-control-unit") generated 3 warnings
error: could not compile `telematics-control-unit` (bin "telematics-control-unit") due to 3 previous errors; 3 warnings emitted
I do not know why I got this error even though I used temporary variables to separate the scope of the mutable borrows, I still got the error.
Very appreciate if there are any solutions
3 posts - 3 participants
🏷️ Rust_feed