A performant and feature-rich recreation of the Unix ls command built with Rust, demonstrating modern systems programming practices and ecosystem tools.
This project serves as an excellent learning resource for several key Rust concepts and ecosystem packages:
- Error Handling: Proper use of
Resulttypes and the?operator for clean error propagation - File System Operations: Working with
std::fsfor directory traversal and metadata extraction - Struct Design: Creating well-structured data with
derivemacros for serialization and display - Memory Management: Efficient memory usage with pre-allocated collections and borrowing
- Purpose: Command-line argument parsing
- Learning: How to create intuitive CLI interfaces with minimal boilerplate
- Features Used:
derivemacros for automatic argument parsing, version/About text
- Purpose: Serialization and deserialization
- Learning: Converting Rust structs to JSON for API-like output formats
- Features Used:
derivemacros, pretty-printed JSON output
- Purpose: Date and time handling
- Learning: Working with timestamps, formatting dates for human-readable output
- Features Used: UTC datetime conversion, custom date formatting
- Purpose: Terminal color output
- Learning: Enhancing user experience with colored error messages and output
- Features Used: Color methods for string styling
- Purpose: Enum enhancements
- Learning: Working with enums that need string representation and serialization
- Features Used:
DisplayandSerializederives for enums
- Purpose: Table formatting for terminal output
- Learning: Creating beautiful, aligned tables with custom styling and colors
- Features Used: Custom column names, styling, and color schemes
- Directory traversal with
fs::read_dir() - Metadata extraction (
fs::metadata()) - File type detection (files vs directories)
- Timestamp handling and formatting
- Pre-allocating vector capacity to reduce reallocations
- Efficient error handling without silent failures
- Borrowing and reference management
- Constants for repeated string literals
- Using
Result<T, E>for recoverable errors - The
?operator for clean error propagation - Structured error handling with
matchstatements - User-friendly error messages with color coding
bestls/
├── src/
│ └── main.rs # Complete implementation in a single file
├── Cargo.toml # Dependencies and project metadata
└── README.md # This file
#[derive(Debug, Parser)]
#[command(version, about, long_about = "Best ls command ever")]
struct Cli {
path: Option<PathBuf>,
#[arg(short, long)]
json: bool,
}Learn how to create professional CLI tools with automatic help generation.
#[derive(Debug, Tabled, Serialize)]
struct FileEntry {
name: String,
e_type: FileType,
len_bytes: u64,
modified: String,
}Understand how to make data serializable and displayable in multiple formats.
fn get_files(path: &PathBuf) -> Result<Vec<FileEntry>, io::Error> {
let read_dir = fs::read_dir(path)?;
// ... error handling with ? operator
}Learn idiomatic Rust error handling patterns.
let mut data = Vec::with_capacity(8); // Pre-allocateDiscover simple but effective optimization techniques.
# Standard table output
cargo run
# JSON output
cargo run -- --json
# Specify directory
cargo run -- /path/to/directory
# JSON for specific directory
cargo run -- --json /path/to/directoryThis recreation of ls demonstrates how to:
- Build practical command-line tools with Rust
- Integrate multiple ecosystem packages effectively
- Handle real-world file system operations safely
- Create user-friendly terminal applications
- Apply performance optimization techniques
Perfect for intermediate Rust developers looking to understand:
- How to structure a real CLI application
- Integration of popular Rust packages
- Systems programming best practices
- Error handling in production code
# Build the project
cargo build
# Run in debug mode
cargo run
# Run with arguments
cargo run -- --json
# Build optimized release
cargo build --releaseThis project serves as both a functional tool and an educational resource for modern Rust development practices.