From 3e6e97e8218568557bec6fc7e3539a9abdd92515 Mon Sep 17 00:00:00 2001 From: panoplied Date: Fri, 27 Jun 2025 01:56:03 +0300 Subject: [PATCH] add Rust implementation of DFS example --- 07_trees/rust/01_filesystem_dfs.rs | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 07_trees/rust/01_filesystem_dfs.rs diff --git a/07_trees/rust/01_filesystem_dfs.rs b/07_trees/rust/01_filesystem_dfs.rs new file mode 100644 index 00000000..58d44ea5 --- /dev/null +++ b/07_trees/rust/01_filesystem_dfs.rs @@ -0,0 +1,34 @@ +use std::fs; +use std::path::Path; + +fn printnames(dir: &Path) { + let entries = match fs::read_dir(dir) { + Ok(entries) => entries, + Err(_) => { + return; + } + }; + + let mut entries: Vec<_> = entries.filter_map(|e| e.ok()).collect(); + entries.sort_by_key(|e| e.file_name()); + + for entry in entries { + let path = entry.path(); + let file_type = match entry.file_type() { + Ok(ft) => ft, + Err(_) => { + continue; + } + }; + + if file_type.is_file() { + println!("{}", entry.file_name().to_string_lossy()); + } else if file_type.is_dir() { + printnames(&path); + } + } +} + +fn main() { + printnames(Path::new("pics")); +}