use std::{fs, path::PathBuf, process, env}; fn find_target_dir(path: PathBuf) -> Result { for entry in fs::read_dir(path)? { let entry = entry?; let p = entry.path(); if p.is_dir() && p.ends_with("target") { return Ok(true); } } Ok(false) } fn execute_clean(path: PathBuf) -> Result<(), std::io::Error> { let _cmd = process::Command::new("cargo") .current_dir(path) .arg("clean") .status()?; Ok(()) } fn clean_in_dir(path: PathBuf) -> Result { let mut cleaned: u64 = 0; for entry in fs::read_dir(path)? { let entry = entry?; let p = entry.path(); if p.is_dir() { let needed = find_target_dir(p.clone())?; if needed { println!(" => {}", p.display()); execute_clean(p)?; cleaned += 1; } } } Ok(cleaned) } fn main() -> Result<(), std::io::Error> { println!("Rust Pan"); let cwd = env::current_dir()?; let cleand = clean_in_dir(cwd)?; println!("Cleaned {} crates", cleand); Ok(()) }