Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Watch A Directory For File Changes In Rust

I'm using `notify_debouncer_mini`` in my Rust apps to watch directories for file changes.

A basic sample looks like this:

Code

use notify_debouncer_mini::{new_debouncer, notify::*};
use std::path::PathBuf;
use std::time::Duration;

fn main() {
    let _ = watch_files();
}


fn watch_files() -> Result<()> {
    let watch_dir = PathBuf::from(".");
    let (tx, rx) = std::sync::mpsc::channel();
    let mut debouncer = new_debouncer(Duration::from_millis(100), tx)?;
    debouncer
        .watcher()
        .watch(&watch_dir, RecursiveMode::Recursive)?;
    for result in rx {
        match result {
            Ok(events) => events.iter().for_each(|event| {
                dbg!(&event.path);
            }),
            Err(_) => {}
        }
    }
    Ok(())
}

References