Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. use std::{
  2. fs::{self, DirEntry},
  3. io,
  4. path::Path,
  5. time::Instant,
  6. };
  7.  
  8. fn main() {
  9. let dir: Vec<&str> = "directory/path".splitn(2, "/").collect();
  10. println!("{:?}", dir[0]);
  11. }
  12. ///walking a directory only visiting files
  13. pub fn visit_dirs(dir: &Path, cb: dyn Fn(&DirEntry)) -> io::Result<()> {
  14. //if path provided leads to a directory
  15. if dir.is_dir() {
  16. for entry in fs::read_dir(dir)? {
  17. let entry = entry?;
  18. let path = entry.path();
  19. //for each entry check if it is dir itself
  20. if path.is_dir() {
  21. //if so, execute recoursively
  22. visit_dirs(&path, cb)?;
  23. } else {
  24. //do stuff with a file
  25. cb(&entry);
  26. }
  27. }
  28. }
  29. Ok(())
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement