Skip to content

Instantly share code, notes, and snippets.

@su8
Created November 7, 2025 23:07
Show Gist options
  • Select an option

  • Save su8/c500af76472c978ffb2363ea6513c8fe to your computer and use it in GitHub Desktop.

Select an option

Save su8/c500af76472c978ffb2363ea6513c8fe to your computer and use it in GitHub Desktop.
main10.rs
use std::fs;
use std::fs::File;
use std::fs::DirEntry;
use std::io;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Mutex;
use std::thread;
use std::collections::HashMap;
static mut mtx: Mutex<()>;
static mut cur_dir_num: HashMap<String, u64> = HashMap::new();
fn walk_multiple_dirs(folder: &str, opt: char) {
for entry in fs::read_dir(folder).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
let mut lock = mtx.lock().unwrap();
fs::set_current_dir(folder).unwrap();
if opt == 'b' {
if path.is_file() {
println!("{} {} bytes", path.file_name().unwrap(), path.metadata().unwrap().len());
}
}
*cur_dir_num.entry(folder.to_string()).or_insert(0) += 1;
}
let cur_folder = if folder.starts_with('.') { fs::current_dir().unwrap().to_str().unwrap().to_string() } else { folder.to_string() };
println!("{} {} items", cur_folder, *cur_dir_num.get(folder).unwrap());
}
fn main() {
let args: Vec<String> = std::env::args().collect();
if args.len() > 1 && (args[1].chars().next().unwrap() == '' || args[1].chars().next().unwrap() == 'b') {
let mut threads = vec![];
for x in 2..args.len() {
threads.push(thread::spawn(move || walk_multiple_dirs(&args[x], args[1].chars().next().unwrap())));
*cur_dir_num.entry(args[x].clone()).or_insert(0);
}
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 io::stdin().lines() {
count += 1;
}
println!("{} items", count);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment