pub fn is_hidden(path: impl AsRef<Path>) -> Result<bool>Expand description
Returns true if the path is a hidden file or directory.
§Platform-specific behavior
- On Unix, returns
trueif the file name starts with.. - On Windows, returns
trueif the file has the hidden file attribute.
§Errors
§On Unix
Returns Err if any of the following are true:
pathterminates in...pathis not valid UTF-8.
§On Windows
Returns Err if metadata about a file could not be obtained.
§Examples
§On Unix
assert_eq!(hf::is_hidden(".foo.txt").unwrap(), true);
assert_eq!(hf::is_hidden("foo.txt").unwrap(), false);
assert!(hf::is_hidden(".foo.txt/..").is_err());§On Windows
let temp_dir = tempfile::tempdir().unwrap();
let file_path = temp_dir.path().join("foo.txt");
assert!(!file_path.exists());
File::create(&file_path).unwrap();
assert!(file_path.exists());
// Set the hidden file attribute.
Command::new("attrib")
.arg("+h")
.arg(&file_path)
.status()
.unwrap();
assert_eq!(hf::is_hidden(&file_path).unwrap(), true);
// Clear the hidden file attribute.
Command::new("attrib")
.arg("-h")
.arg(&file_path)
.status()
.unwrap();
assert_eq!(hf::is_hidden(&file_path).unwrap(), false);
fs::remove_file(&file_path).unwrap();
assert!(hf::is_hidden(file_path).is_err());