Fs operations vs Fire program commands

Hello, I'm Bylka, a new rust developer.

I'm trying to build a CLI to do automate some repeated tasks while building some big projects, and for that I'm trying to do some operations on the files and dirs. So my question is around this, is it better to use the std::fs API provided by the core library of rust, or it would be more beneficial to use the pre made commands in the OS level to make those tasks?

Thanks for your time.

If you are asking whether you should shell out to external binaries: no. That's incredibly non-portable.

1 Like

If possible, try to use functions from the standard library rather than assuming you'll have access to commands like rm or cp or mkdir. Assuming you'll have access to all the same programs and that they'll always behave the same is why so many projects work fine on Linux and MacOS, but crash when running on Windows.

Have a look at the xtask pattern for doing these sorts of management tasks. You might also want to use the xshell crate for the low level filesystem operations.

2 Likes

It's less common, but you can also see Linux and macOS working differently. Linux distributions usually ship with GNU command-line programs, whereas macOS’s are descended from BSD (and occasionally have their own Apple-specific extensions or quirks). This often shows up in specific commands having slightly different options.

1 Like

Thanks for your answer!

So what i understand from u're reply is the most suitable way to do this kinda of operations using the core library of rust, since i can benefit the cross platform compatibility with it. right?

Yes, that's correct.

It's also going to be faster 9 times out of 10, but considering we're talking about operations that are normally measured using milliseconds, I doubt anyone will care.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.