Last active
November 8, 2025 00:14
-
-
Save su8/0ed048b1ff0062669a5d5cc23d34e345 to your computer and use it in GitHub Desktop.
main11.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::fs; | |
| use std::fs::DirEntry; | |
| use std::fs::File; | |
| use std::fs::OpenOptions; | |
| use std::io; | |
| use std::io::Write; | |
| use std::path::Path; | |
| use std::path::PathBuf; | |
| use std::process; | |
| use std::sync::Mutex; | |
| use std::thread; | |
| struct DirStats { | |
| dir_name: String, | |
| dir_size: u64, | |
| } | |
| fn walk_multiple_dirs(folder: String, opt: char) { | |
| let mut dir_stats = DirStats { | |
| dir_name: folder.to_string(), | |
| dir_size: 0, | |
| }; | |
| for entry in fs::read_dir(folder).unwrap() { | |
| let entry = entry.unwrap(); | |
| let path = entry.path(); | |
| let file_name = path.file_name().unwrap().to_str().unwrap(); | |
| if opt == 'b' { | |
| if path.is_dir() { | |
| continue; | |
| } | |
| dir_stats.dir_size += path.metadata().unwrap().len(); | |
| } | |
| dir_stats.dir_size += path.metadata().unwrap().len(); | |
| } | |
| println!("{} {} items", dir_stats.dir_name, dir_stats.dir_size); | |
| } | |
| fn main() { | |
| let args: Vec<String> = std::env::args().collect(); | |
| if args.len() > 1 && (args[1].chars().next().unwrap() == 'm' || args[1].chars().next().unwrap() == 'b') { | |
| let mut threads = Vec::new(); | |
| for x in 2..args.len() { | |
| let folder = args[x].to_string(); | |
| let opt: char = args[1].chars().next().unwrap(); | |
| threads.push(thread::spawn(move || walk_multiple_dirs(folder, opt))); | |
| } | |
| for thread in threads { | |
| thread.join().unwrap(); | |
| } | |
| } else if args.len() > 1 && args[1].chars().next().unwrap() == 'l' { | |
| let mut count = 0; | |
| for line in std::io::stdin().lines() { | |
| count += 1; | |
| } | |
| println!("{}", count); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment