by ComputerGuru · 2y ago · 0 comments
It's not a universal "for all compiled langs," but I published a two-liner [0] with zero (non-standard) dependencies or pre-requisites that can be copy-and-pasted to the top of any rust code to turn it into an executable script (cached! also correctly cache-invalidating!) back in 2020.
It even lets you optionally omit the `fn main() { ... }` surrounding your code as well, if you really want to feel like you're just shell scripting <insert appropriate emoji here>.
It's an example of "polyglot source code" that is valid under two languages at once (in this case, standard sh and rust), (ab)using the rust `#[allow(...)]` "macro" to do double-duty as a shell script comment opener.
The clean solution of cargo script is here: https://github.com/rust-lang/cargo/issues/12207
Following the links, the RFC contains a nice list of similar tools: https://rust-lang.github.io/rfcs/3424-cargo-script.html#prio...
> Would it have been possible back then to use cargo to pull in some dependencies?
Not at the time, at least not that I know of.
I wonder if it's possible to get a link to this solution added to that "prior art" section.
Tiny nitpick: the #![allow()] generates a 'warning: unused attribute' warning for me.
If you change it to #![allow(unused_attributes)] it disables the warning for itself.