0

I have this code:

fn main() {
    let root = std::path::Path::new("resources/");
    let uri = std::path::Path::new("/js/main.js");
    let path = root.join(uri);
    assert_eq!(path.to_str(), Some("resources/js/main.js"));
}

As stated in Why does joining paths completely replace the original path in Rust?, this will fail because of the first slash in /js/main.js. It will work only without the slash in the beginning.

Is there a way to avoid this behaviour, even if the slash is put?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
EuberDeveloper
  • 874
  • 1
  • 14
  • 38
  • [The duplicate applied to your situation](https://play.integer32.com/?version=stable&mode=release&edition=2021&gist=f6b2b51456e9765a02d79d5eed7a2636). This was also [explained in a comment on the question you linked to](https://stackoverflow.com/questions/53430401/why-does-joining-paths-completely-replace-the-original-path-in-rust#comment93742745_53430667) – Shepmaster Dec 20 '21 at 14:43
  • It is true that it answers my question, but by searching (with my point of view) I did not find that post, this means that while the answer is already present, finding it by typing my question did not show it – EuberDeveloper Dec 21 '21 at 15:48
  • That's perfect then! The _entire point_ of duplicate questions on SO is to serve as signposts to a canonical answer. Now, when anyone searches using the terms that you used in your question, they can click on this question and then be redirected to the existing answer. To state it another way: **there's nothing inherently *bad* about duplicates**. – Shepmaster Dec 21 '21 at 15:58

0 Answers0