My first interaction with any runtime which touched the filesystem and paths was in node.js many years ago.
I have to admire the path module in node.js for its simplicity. I had to assume that other languages wouldn't do things much differently.
This year I tried c++ (kind of love it even when it gets really really really verbose and hard to follow).
Last week I picked up Rust, I wanted c++ but with a friendly face. So far I am getting the usual wobbles, "should I give up? change language again? This is also hard, I love the speed though, I can understand some of what I'm writing..." but I stick in there, I am trying to write a simple CLI tool that does nothing more than concat an input path to a current working directory, think pwd + input_path
. In node that would be trivial and I could resolve paths like this:
const userInput = argv // maybe something like this "../";
path.resolve(process.cwd(), userInput); // noice!
But googling brings me no closer, Cargo has no crates (packages) I could understand to have similar resolve functionality.
My options
0: You help me to see Im doing something wrong.
1: So do I concede that rust is not for me.
2: Try V lang
3: Use LuaJit
4: Slow but productive Use Typescript
I am quite unsure.
Top comments (22)
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, likeString
, andPath
is a reference to a slice, likestr
.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.
Yep, that's exactly what I was getting at. You literally just
push
aPath
to aPathBuf
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!Ah, sorry, you're probably specifically looking for canonicalize?
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.
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.
Also one last thing, what the heck is Ok(()), I keep seeing this in the docs.
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.
That's an artifact of how rust generally handles errors - all functions return a
Result<T, E>
. The variants of this type areOk(T)
orErr(E)
. Functions that don't return a value end up with anOk(())
, signalling success and returning unit. You can only use?
in a fn that returns aResult
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.
Not sure how serious you are, but "V Lang" is essentially a barely functional pile of broken promises and shouldn't be used for even toy work.
If you're interested in low-level languages that are more friendly than C++ (and that's not Rust), try D. Or, for a very young and yet unstable language, try Zig.
I will take a look at Zig
Interesting, I got the impression after reading the release date ... "umm theres a very specific release date here, this language is not ready is it..."
Nim lang one-liner Std Lib:
Use:
(Its 2 Lines, but
import
can be passed on command with--import:os
, so is 1)Passing command line imports that's cheeting, still two lines just injected haha. I got the rust thing working thanks to Ben.
But you dont have to pass command line imports, you can use a
file.nim.cfg
:But it was not about Code Golf anyways.
👑
Well played 😁
What have you got so far? Would help to see what code you have, so we can figure out where to go from that.
I don't think it really helps sharing code sometimes, you either know what I'm doing or you don't and if you do, you have done it before. Not to say that you haven't Yawer, it's just a tend to get better potential answers. I will share the working code. Ben helped me out big time.
Fair enough, on re-reading your post it seems you just need doc.rust-lang.org/std/env/fn.curre... and doc.rust-lang.org/std/path/struct.... to join together the current working directory and a string directory.
Thanks that is a good lead 😁
I am new to Rust from NodeJS. I was facing the same issue. This is the code example i came up with.