What Is OsString and When Should I Use It?

Use OsString with std::env::args_os to handle command line arguments containing invalid Unicode without panicking.

Use OsString when handling command line arguments or file paths that might contain invalid Unicode, as it prevents the panic that occurs with std::env::args. Use std::env::args_os to retrieve an iterator of OsString values instead of String values:

use std::env;

fn main() {
    let args: Vec<OsString> = env::args_os().collect();
}