• FizzyOrange@programming.dev
      link
      fedilink
      arrow-up
      17
      ·
      2 days ago

      Yeah it’s great for little scripts. There’s even a cargo script feature that’s being worked on so you can compile & run them using a shebang.

      I’d use a shell script if it is literally just a list of commands with no control logic or piping. Anything more than that and you’re pointing a loaded gun at your face, and should switch to a proper language, of which Rust is a great choice.

    • 5C5C5C@programming.dev
      link
      fedilink
      arrow-up
      34
      ·
      2 days ago

      Honestly yes. If I need to manipulate the filesystem or manage processes with any amount of conditional logic or looping, I’d much rather do it with Rust than shell scripts.

      The only thing I use shell scripts for anymore is completely trivial sequences of commands.

        • Ephera@lemmy.ml
          link
          fedilink
          English
          arrow-up
          13
          ·
          2 days ago

          One of the simplest tricks is that you can throw down a function, which you can call with a command like e.g. this: run("cat /etc/os-release | grep NAME")
          by constructing a Command like so:

          Command::new("sh")
              .arg("-c")
              .arg(command) //the string passed as parameter
          

          There’s proper libraries to make running commands even easier and more robust, but if you don’t want to pull in a library, that’s really easy to write out ad-hoc and gets you 95% of the way there, with shell piping and everything.

            • FizzyOrange@programming.dev
              link
              fedilink
              arrow-up
              6
              ·
              16 hours ago

              That’s kind of the point. You can do it in most languages, so why use a shitty one like Bash? Use a good language like Rust!

              Also there are aspects of languages that make many languages less suitable for this application though. For example Python, because you can’t use third party dependencies (or at least you couldn’t; I think uv has an equivalent of cargo script now). Java would be a pretty awful choice for example.

              • 1984@lemmy.today
                link
                fedilink
                arrow-up
                1
                ·
                edit-2
                15 hours ago

                Python is actually super known for its batteries included approach, it has modules for everything. Simply do pip install anything. But best practice is to use a python virtual environment and install packages into that one. Cargo does this by itself in each project and doesnt install modules globally.

                But python code is so much easier to write. Its basically almost English and the syntax is easy. Rust… Not so much. Its quite ugly.

                Its a systems programming language. Designed to be fast to execute. Its one of the slowest to write code in. But sure, with Ai, you can just ask for a rust script and it will run.

                • FizzyOrange@programming.dev
                  link
                  fedilink
                  arrow-up
                  3
                  ·
                  14 hours ago

                  it has modules for everything

                  Not everything. PyYAML, Pydantic and Typer are things I commonly want in scripts that aren’t in the standard library.

                  Simply do pip install anything. But best practice is to use a python virtual environment and install packages into that one.

                  It’s more than “best practice”. It’s mandatory on many recent Linux distros. And yeah setting up a venv and installing dependencies is not something you want to have to do for each script you run.

                  Its one of the slowest to write code in.

                  It depends what your goal is. If you want robust code that works reliably then I would say Rust has the edge still. Yes it will take longer to write but you’ll spend way less time debugging it and writing tests.

            • Ephera@lemmy.ml
              link
              fedilink
              English
              arrow-up
              11
              ·
              2 days ago

              Oh yeah, I’m not saying this is what makes Rust special. Rust’s strength in comparison to Bash is that it’s a lot more competent at control flow and structuring programs. But yeah, virtually any programming language is at least better at that than Bash, so whichever one you’re most comfortable with, is probably the best choice. This trick just allows you to make use of Bash’s biggest strengths, which is easily running commands and piping between commands, while also having the competent control flow and structuring of your programming language of choice.

    • 1984@lemmy.today
      link
      fedilink
      arrow-up
      2
      arrow-down
      6
      ·
      2 days ago

      Kind of agree with you here… I wouldnt reach for rust. Python gives much faster results than rust.