Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Convert from `&PathBuf` to `PathBuf`

This seems to be a very simple question, but I haven’t got a clue from the documentation. The code creates a PathBuf from &PathBuf. I wonder what makes this possible. The documentation has an implementation that creates a String from &String, but doesn’t have one such implementation for PathBuf? What’s happening behind the scene? And what tools/methods can help me debug/investigate?

fn main() {
    let a = std::path::PathBuf::from("");
    let b = std::path::PathBuf::from(&a);
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It goes via OsStr.

PathBuf has an implementation impl AsRef<OsStr> for Path and an implementation impl<T: AsRef<OsStr>> From<&T> for PathBuf

Incidentally, this is how the case works for a as well. PathBuf doesn’t implement From<&str> but &str does implement AsRef<OsStr>.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading