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 :
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>.