Created
January 28, 2023 13:44
-
-
Save tuncatunc/b9bbbc048aca31caf183ceec1ad68e71 to your computer and use it in GitHub Desktop.
ls in rust
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::path::Path; | |
fn list_files(path: &Path) { | |
let metadata = match fs::metadata(path) { | |
Ok(metadata) => metadata, | |
Err(_) => return, | |
}; | |
if metadata.is_file() { | |
let file_size = metadata.len(); | |
println!("{:10} {:?}", file_size, path.display()); | |
} else if metadata.is_dir() { | |
let paths = match fs::read_dir(path) { | |
Ok(paths) => paths, | |
Err(_) => return, | |
}; | |
for path in paths { | |
let path = path.unwrap().path(); | |
list_files(&path); | |
} | |
} | |
} | |
fn main() { | |
let path = Path::new("."); | |
list_files(path); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment