题:。如何在 Rust 中观察文件/目录的变化?额外:如何以非阻塞方式集成它。规范示例(例如由https://docs.rs/notify/4.0.15/notify/提供的示例)显示了如何查看文件,但它会阻止主函数执行的其余部分。(我正在追求这个https://doc.rust-lang.org/std/sync/mpsc/fn.channel.html)
notify板条箱的示例代码可以满足您的需求。它用于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),
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3194 次 |
| 最近记录: |