How to do it...

  1. Open the Cargo.toml file that was generated earlier for you.

  2. Under [dependencies], add the following line:
walkdir = "2.0.1"
  1. If you want, you can go to walkdir's crates.io page (https://crates.io/crates/walkdir) to check for the newest version and use that one instead.
  2. In the bin folder, create a file called traverse_files.rs.

  3. Add the following code and run it with cargo run --bin traverse_files:

1    extern crate walkdir;
2 use walkdir::{DirEntry, WalkDir};
3
4 fn main() {
5 println!("All file paths in this directory:");
6 for entry in WalkDir::new(".") {
7 if let Ok(entry) = entry {
8 println!("{}", entry.path().display());
9 }
10 }
11
12 println!("All non-hidden file names in this directory:");
13 WalkDir::new("../chapter_three")
14 .into_iter()
15 .filter_entry(|entry| !is_hidden(entry)) // Look only at
non-hidden enthries
16 .filter_map(Result::ok) // Keep all entries we have access to
17 .for_each(|entry| {
18 // Convert the name returned by theOS into a Rust string
19 // If there are any non-UTF8 symbols in it, replace them
with placeholders
20 let name = entry.file_name().to_string_lossy();
21 println!("{}", name)
22 });
23
24 println!("Paths of all subdirectories in this directory:");
25 WalkDir::new(".")
26 .into_iter()
27 .filter_entry(is_dir) // Look only at directories
28 .filter_map(Result::ok) // Keep all entries we have
access to
29 .for_each(|entry| {
30 let path = entry.path().display();
31 println!("{}", path)
32 });
33
34 let are_any_readonly = WalkDir::new("..")
35 .into_iter()
36 .filter_map(Result::ok) // Keep all entries we have
access to
37 .filter(|e| has_file_name(e, "vector.rs")) // Get the
ones with a certain name
38 .filter_map(|e| e.metadata().ok()) // Get metadata if the
OS allows it
39 .any(|e| e.permissions().readonly()); // Check if at
least one entry is readonly
40 println!(
41 "Are any the files called 'vector.rs' readonly? {}",
42 are_any_readonly
43 );
44
45 let total_size = WalkDir::new(".")
46 .into_iter()
47 .filter_map(Result::ok) // Keep all entries we have access
to
48 .filter_map(|entry| entry.metadata().ok()) // Get metadata
if supported
49 .filter(|metadata| metadata.is_file()) // Keep all files
50 .fold(0, |acc, m| acc + m.len()); // Accumulate sizes
51
52 println!("Size of current directory: {} bytes", total_size);
53 }
  1. Now, come to the predicates used in this recipe:
55   fn is_hidden(entry: &DirEntry) -> bool {
56 entry
57 .file_name()
58 .to_str()
59 .map(|s| s.starts_with('.'))
60 .unwrap_or(false) // Return false if the filename is
invalid UTF8
61 }
62
63 fn is_dir(entry: &DirEntry) -> bool {
64 entry.file_type().is_dir()
65 }
66
67 fn has_file_name(entry: &DirEntry, name: &str) -> bool {
68 // Check if file name contains valid unicode
69 match entry.file_name().to_str() {
70 Some(entry_name) => entry_name == name,
71 None => false,
72 }
73 }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.194