• FizzyOrange@programming.dev
      link
      fedilink
      arrow-up
      1
      ·
      2 minutes 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
      14
      ·
      8 hours 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
          3
          ·
          7 hours 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.

  • Ephera@lemmy.ml
    link
    fedilink
    English
    arrow-up
    1
    ·
    6 hours ago

    “I think proc macros are a really big superpower for Rust.”

    Yeah, been working on a framework and proc-macros are really useful to simplify the API.

    For example, previously I needed users to define a struct and then an impl block with a function inside. Well, and they need to do that a lot, so it was genuinely a bit of a pain to write out, but it also made the code feel more complex than it really was.

    Now I’ve got an annotation, which you can slap onto a function and then it generates the struct from the function parameters and puts the function into the impl block.
    And while you do need to be aware that the function parameters will be put into a struct and therefore you want owned parameters, all-in-all it still feels really nice to just delete dozens of boilerplate lines and an indentation level in the codebases where I’ve introduced it.