DEV Community

Discussion on: Rust working with paths

Collapse
 
deciduously profile image
Ben Lovy

I think std::path provides all the methods you're looking for. Did you try anything from here and have trouble with it? PathBuf is owned, like String, and Path is a reference to a slice, like str.

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

Thanks Ben, I found a join method in the Path std lib, that looks fine, but Im scratching my head, there is no resolve.

I honestly don't know a lot about Rust but I think you are trying to tell me that PathBuff can do string things. I dont even know if I can split strings and mess with them as a vec.

Collapse
 
deciduously profile image
Ben Lovy • Edited

Yep, that's exactly what I was getting at. You literally just push a Path to a PathBuf and there it is, and now it's been attached as a child directory. No need to resolve anything else.

String has a split() - that should help!

Collapse
 
deciduously profile image
Ben Lovy

Ah, sorry, you're probably specifically looking for canonicalize?

Thread Thread
 
adam_cyclones profile image
Adam Crockett 🌀

Probably, word on the street (StackOverflow) is that there are issues with canonicalize (I should find out more), thanks for the push, I will go check it out.

Thread Thread
 
adam_cyclones profile image
Adam Crockett 🌀

also, interesting that this is part of the fs module. As I said before, I am used to node where this would be part of the path module.

Thread Thread
 
adam_cyclones profile image
Adam Crockett 🌀 • Edited

Also one last thing, what the heck is Ok(()), I keep seeing this in the docs.

Thread Thread
 
deciduously profile image
Ben Lovy • Edited

I hadn't seen any issues with it in my own usage, but there's also path_abs, an external crate which solves this problem. So, the existence of the crate says that there probably is something I don't know about! Good luck.

Thread Thread
 
deciduously profile image
Ben Lovy • Edited

That's an artifact of how rust generally handles errors - all functions return a Result<T, E>. The variants of this type are Ok(T) or Err(E). Functions that don't return a value end up with an Ok(()), signalling success and returning unit. You can only use ? in a fn that returns a Result

Thread Thread
 
adam_cyclones profile image
Adam Crockett 🌀

So it looks like the airquotes problem with canonicalize is that it looks at the real FS. In a cli tool where foo/ is not a dir this would panic (that to me is a real strength!) This is substantially better than node path where it's just a string. Just need catch the error. IL share working code soon.