Created
December 18, 2020 16:51
-
-
Save bynect/7b51c7c342a2206c5faf9eaed77d4f27 to your computer and use it in GitHub Desktop.
Educational implementation of the ls command in Rust. Doesn't support globbing nor proper error handling.
This file contains 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
pub fn main() { | |
// collect the command line arguments into a vector of string | |
let mut argv: Vec<String> = std::env::args().collect(); | |
// if no arguments are given assume the current directory | |
if argv.len() < 2 { | |
argv.push(String::from(".")); | |
} | |
// iterate over the vector of args ignoring the first argument | |
for arg in &argv[1..] { | |
// iterate over every entry in the dir | |
for fd in std::fs::read_dir(arg).expect("Couldn't read directory.") { | |
// get the entry name | |
let name = fd.expect("Couldn't read entry.").file_name(); | |
let name = name.to_string_lossy(); | |
// ignore entry starting with a dot | |
if !name.starts_with(".") { | |
// print entry name without newline | |
print!("{} ", name); | |
} | |
} | |
// print a trailing newline for every argument | |
println!(""); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment