Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing a path in Rust

Tags:

rust

How could I print a path in Rust?

I tried the following in order to print the current working directory:

use std::os;

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

But rustc returns 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
like image 661
wei2912 Avatar asked Jun 05 '14 10:06

wei2912


1 Answers

As you discovered, the "correct" way to print a Path is via the .display method, which returns a type that implements Display.

There is a reason Path does not implement Display itself: formatting a path to a string is a lossy operation. Not all operating systems store paths compatible with UTF-8 and the formatting routines are implicitly all dealing with UTF-8 data only.

As an example, on my Linux system a single byte with value 255 is a perfectly valid filename, but this is not a valid byte in UTF-8. If you try to print that Path to a string, you have to handle the invalid data somehow: .display will replace invalid UTF-8 byte sequences with the replacement character U+FFFD, but this operation cannot be reversed.

In summary, Paths should rarely be handled as if they were strings, and so they don't implement Display to encourage that.

like image 72
huon Avatar answered Oct 06 '22 02:10

huon