Print path in Rust

How can I print a path in Rust?

I tried the following to print the current working directory:

use std::os;

fn main() {
    let p = os::getcwd();
    println!("{}", p);
}

But it rustcreturns with the following error:

[wei2912@localhost rust-basics]$ rustc ls.rs 
ls.rs:5:17: 5:18 error: failed to find an implementation of trait core::fmt::Show for std::path::posix::Path
ls.rs:5     println!("{}", p);
                           ^
note: in expansion of format_args!
<std macros>:2:23: 2:77 note: expansion site
<std macros>:1:1: 3:2 note: in expansion of println!
ls.rs:5:2: 5:20 note: expansion site
+5
source share
3 answers

As you have discovered, the “correct” way of printing Pathis a method that returns a type that implements . .display Display

There is a reason why it Pathdoes not implement itself Display: formatting the path to a string is a lossy operation. Not all operating systems store UTF-8 compatible paths, and formatting procedures implicitly all deal only with UTF-8 data.

, Linux 255 , UTF-8. Path , - : .display UTF-8 U + FFFD, .

, Path , , Display .

+16

:

println!("{}", p.display());

. Path::display.

+4

, , " UTF-8" stfu8.

escape- (, \x00) UTF-8, .

0
source

Source: https://habr.com/ru/post/1543394/


All Articles