improve first example in the docs (#543)

This commit is contained in:
Kent Ross
2024-01-05 01:07:52 +09:00
committed by GitHub
parent 2437896199
commit e0902a1101
+19 -8
View File
@@ -102,16 +102,17 @@
//!
//! ```rust
//! # use std::path::Path;
//! use notify::{Watcher, RecommendedWatcher, RecursiveMode, Result};
//! use notify::{recommended_watcher, Event, RecursiveMode, Result, Watcher};
//! use std::sync::mpsc;
//!
//! fn main() -> Result<()> {
//! // Automatically select the best implementation for your platform.
//! let mut watcher = notify::recommended_watcher(|res| {
//! match res {
//! Ok(event) => println!("event: {:?}", event),
//! Err(e) => println!("watch error: {:?}", e),
//! }
//! })?;
//! let (tx, rx) = mpsc::channel::<Result<Event>>();
//!
//! // Use recommended_watcher() to automatically select the best implementation
//! // for your platform. The `EventHandler` passed to this constructor can be a
//! // closure, a `std::sync::mpsc::Sender`, a `crossbeam_channel::Sender`, or
//! // another type the trait is implemented for.
//! let mut watcher = notify::recommended_watcher(tx)?;
//!
//! // Add a path to be watched. All files and directories at that path and
//! // below will be monitored for changes.
@@ -123,6 +124,16 @@
//! # { // "." doesn't exist on BSD for some reason in CI
//! watcher.watch(Path::new("."), RecursiveMode::Recursive)?;
//! # }
//! # #[cfg(any())]
//! # { // don't run this in doctests, it blocks forever
//! // Block forever, printing out events as they come in
//! for res in rx {
//! match res {
//! Ok(event) => println!("event: {:?}", event),
//! Err(e) => println!("watch error: {:?}", e),
//! }
//! }
//! # }
//!
//! Ok(())
//! }