{"id":263,"date":"2022-09-27T21:05:00","date_gmt":"2022-09-28T02:05:00","guid":{"rendered":"http:\/\/sycured.127.0.0.1.sslip.io\/?p=263"},"modified":"2024-01-14T12:19:54","modified_gmt":"2024-01-14T17:19:54","slug":"rewriting-scripts-in-rust","status":"publish","type":"post","link":"http:\/\/10.42.0.68:8080\/blog\/rewriting-scripts-in-rust","title":{"rendered":"Rewriting scripts with Rust"},"content":{"rendered":"\n

It’s becoming common to see some tools rewrote with Rust like bat<\/a>, dust<\/a>, tokei<\/a> & co.<\/p>\n\n\n\n

In my case, it’s rewriting some Bash and Python mostly to extend them more easily.<\/p>\n\n\n\n\n\n\n\n

Bash to Rust<\/h2>\n\n\n\n

Bash script can become a real pain, for example when I’m writing it on macOS (bash<\/code> and zsh<\/code> are installed) and needs to execute it on some OS with different shells like (t)csh, ksh.<\/p>\n\n\n\n

Just exporting an environment variable is a rewrite from export NAME=VALUE<\/code> to setenv NAME VALUE<\/code>, now I let you imagine when it’s directly logical to find available tools like checking if curl<\/code> or wget<\/code> is available and some logic about potential failures.<\/p>\n\n\n\n

With Rust, I can do a strict “no external dependency” so the binary built will always work as expected.
In addition, the type checking permits avoiding a lot of “bargains” specific to the Bash script.<\/p>\n\n\n\n

Python to Rust<\/h2>\n\n\n\n

Python script needs to have Python installed. Sometimes, it’s impossible to have it (ITIL, specific environment requirements & co.) so I need to provide a virtual env.
Providing a 50 MB virtual env for a 60 kB script, it’s unoptimized and mostly a waste of resources\/time.<\/p>\n\n\n\n

The binary version (Rust): 3.2 MB\u2026 so a 93.6% decrease.
I can use scratch<\/code> image as the base for my containers because the binary built is statically linked and doesn’t need an external system like alpine<\/code> or busybox<\/code>.<\/p>\n\n\n\n

The other advantage is for creating an OCI image. Python-based containers are heavy-sized and need some environment variables to work correctly:<\/p>\n\n\n\n