| Crates.io | filelock |
| lib.rs | filelock |
| version | 0.4.2 |
| created_at | 2021-09-06 15:35:28.421504+00 |
| updated_at | 2025-12-04 12:08:06.819245+00 |
| description | Cross-platform file locking library for Rust |
| homepage | |
| repository | https://github.com/aisk/filelock |
| max_upload_size | |
| id | 447575 |
| size | 21,870 |
Simple filelock library for rust, using flock on Unix-like systems and LockFileEx on Windows under the hood.
Image by Homutan, source: https://www.pixiv.net/artworks/128080460
$ cargo add filelock
use filelock;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut lock = filelock::new("myfile.lock");
let _guard = lock.lock()?;
// Perform critical operations
// Lock is automatically released when _guard goes out of scope
Ok(())
}
For manual control:
use filelock;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut lock = filelock::new("myfile.lock");
let guard = lock.lock()?;
// Perform critical operations
// Manually unlock with error handling
guard.unlock()?;
Ok(())
}
See https://docs.rs/filelock/latest/filelock/.
Filelock is distributed by a MIT license.