Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
24
Task/Unix-ls/Rust/unix-ls.rust
Normal file
24
Task/Unix-ls/Rust/unix-ls.rust
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
use std::{env, fmt, fs, process};
|
||||
use std::io::{self, Write};
|
||||
use std::path::Path;
|
||||
|
||||
fn main() {
|
||||
let cur = env::current_dir().unwrap_or_else(|e| exit_err(e, 1));
|
||||
let arg = env::args().nth(1);
|
||||
print_files(arg.as_ref().map_or(cur.as_path(), |p| Path::new(p)))
|
||||
.unwrap_or_else(|e| exit_err(e, 2));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn print_files(path: &Path) -> io::Result<()> {
|
||||
for x in try!(fs::read_dir(path)) {
|
||||
println!("{}", try!(x).file_name().to_string_lossy());
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn exit_err<T>(msg: T, code: i32) -> ! where T: fmt::Display {
|
||||
writeln!(&mut io::stderr(), "{}", msg).expect("Could not write to stderr");
|
||||
process::exit(code)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue