如何以递归方式查看Rust中的文件更改? [关闭]

问题描述 投票:-1回答:1

我想开发一个解析器,当某个目录下的文件发生更改(递归)时会触发该解析器。什么是最好的方式去?

linux rust
1个回答
2
投票

notify crate的示例代码可以满足您的需求。它使用RecursiveMode::Recursive指定在提供的路径中观察所有文件和子目录。

use notify::{Watcher, RecursiveMode, watcher};
use std::sync::mpsc::channel;
use std::time::Duration;

fn main() {
    // Create a channel to receive the events.
    let (sender, receiver) = channel();

    // Create a watcher object, delivering debounced events.
    // The notification back-end is selected based on the platform.
    let mut watcher = watcher(sender, Duration::from_secs(10)).unwrap();

    // Add a path to be watched. All files and directories at that path and
    // below will be monitored for changes.
    watcher.watch("/path/to/watch", RecursiveMode::Recursive).unwrap();

    loop {
        match receiver.recv() {
           Ok(event) => println!("{:?}", event),
           Err(e) => println!("watch error: {:?}", e),
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.