Closed
Description
Throughout chapter 12 (IO Example Project) std::env::args
is used to gather command line arguments and collect them into a Vec<String>
. But this can panic!
if a filename/path is passed in that contains invalid UTF-8 (which is relatively common on *nix based OSs).
It's not for me to decide, but I'd recommend either mentioning this "flaw" in the book, or using something like std::env::args_os
. The args_os
is arguably more complicated than that simple example wants to get into, although it would show how to "correctly" handle command line applications which need to support filenames/paths that can contain invalid UTF-8.
There are several ways to reduce the complexity using args_os
, if certain points can be glossed over. For example,
let files: Vec<PathBuf> = std::env::args_os()
.map(std::path::Path::new)
.map(ToOwned::to_owned)
.collect();