Skip to content

Example in chapter 12 of the new book can panic #378

Closed
@kbknapp

Description

@kbknapp

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();

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions