expression

Native arguments: preserve a path with args_os

An OS argument can contain data that is not valid Unicode. Convert directly into PathBuf for file paths; use lossy conversion only when displaying text to people. The supplied path is not opened. Fixture arguments: ['notes draft.txt']. Expected stdout (not executed): path preserved Common mistake: Using args() for arbitrary file paths, which can panic on invalid Unicode.

Formal Syntax & Structure

rust syntaxrust
std::env::args_os().nth(1).map(std::path::PathBuf::from)

Working Example

Example Snippetrust
fn main() {    let path = std::env::args_os().nth(1).map(std::path::PathBuf::from).expect("supply a path");    assert_eq!(path, std::path::PathBuf::from("notes draft.txt"));    println!("path preserved");}
Rust Reference