# Command-Line Rust

## Metadata
- Author: Ken Youens-Clark
- Full Title: Command-Line Rust
- Category: #books
- Summary: The book teaches how to build command-line tools in Rust. Each chapter has you implement a Unix tool like echor, grepr, basename, and dirname using tests and Cargo. You learn Rust IO, argument parsing, and test-driven development while running cargo test and extending features.
## Highlights
- The more I used statically typed languages like Rust, the more I realized that dynamically typed languages force much more work onto me, requiring me to verify my programs and write many more tests. I gradually came to feel that the Rust compiler, while very strict, was my dance partner and not my enemy. Granted, it’s a dance partner who will tell you every time you step on their toes or miss a cue, but that eventually makes you a better dancer, which is the goal after all. Generally speaking, when I get a Rust program to compile, it usually works as I intended. ([View Highlight](https://read.readwise.io/read/01k5ycv1bzw2mvn00zsxthwcy3))
- Second, it’s easy to share a Rust program with someone who doesn’t know Rust or is not a developer at all. If I write a Python program for a workmate, I must give them the Python source code to run and ensure they have the right version of Python and all the required modules to execute my code. In contrast, Rust programs are compiled directly into a machine-executable file. ([View Highlight](https://read.readwise.io/read/01k5ycvx4bsv81xy38dhmkr0k7))
- Third, I often build containers using Docker or Singularity to encapsulate workflows. I find that the containers for Rust programs are often orders of magnitude smaller than those for Python programs. For instance, a Docker container with the Python runtime may require several hundred MB. In contrast, I can build a bare-bones Linux virtual machine with a Rust binary that may only be tens of MB in size. Unless I really need some particular features of Python, such as machine learning or natural language processing modules, I prefer to write in Rust and have smaller, leaner containers. ([View Highlight](https://read.readwise.io/read/01k5ycwvyqfxgyxw0kkf7hnfvf))
- Finally, I find that I’m extremely productive with Rust because of the rich ecosystem of available modules. ([View Highlight](https://read.readwise.io/read/01k5ycx60jrpn6rvf2b577nded))
- I can use the crate [assert_cmd](https://oreil.ly/Lw-gr) to find the program in my crate directory. I will also add the crate [pretty_assertions](https://oreil.ly/VqD62) to use a version of the assert_eq! macro that shows differences between two strings better than the default version. ([View Highlight](https://read.readwise.io/read/01k5ydcmtpgfz4w86frc4t7te4))
- The Portable Operating System Interface (POSIX) standards dictate that the standard exit code is 0 to indicate success (think zero errors) and any number from 1 to 255 otherwise. ([View Highlight](https://read.readwise.io/read/01k5yddrmwneycj63abfyjsabg))
- The [unit type](https://oreil.ly/BVKGJ) is like an empty value and is signified with a set of empty parentheses: (). The documentation says this “is used when there is no other meaningful value that could be returned.” It’s not quite like a null pointer or undefined value in other languages, a concept first introduced by Tony Hoare (no relation to Rust creator Graydon Hoare), who called the null reference his “billion-dollar mistake.” ([View Highlight](https://read.readwise.io/read/01k5ye0ggcv601x5c94n4hqyd9))
- A trait in Rust is a way to define the behavior of an object in an abstract way. If an object implements the Display trait, then it can be formatted for user-facing output. ([View Highlight](https://read.readwise.io/read/01k5ye1z8yytpnzahe3jh93asr))
- A consequence of Rust placing the dependencies into target is that this directory is now quite large. You can use the disk usage command du -shc . to find that the project now weighs in at about 35 MB, and almost all of that lives in target/debug/deps. ([View Highlight](https://read.readwise.io/read/01k5ye58mv8apf0wjx7xhdj4vk))