4 ms·
For the rustaceans in the crowd: It's not a universal "for all compiled langs," but I published a two-liner [0] with zero (non-standard) dependencies or pre-re
by ComputerGuru 3y ago
For the rustaceans in the crowd:
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.
[0] https://neosmart.net/blog/self-compiling-rust-code/ https://neosmart.net/blog/self-compiling-rust-code/
- qznc 3y agoNice hack! Would it have been possible back then to use cargo to pull in some dependencies? The clean solution of cargo script is here: https://github.com/rust-lang/cargo/issues/12207 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#prior-art https://rust-lang.github.io/rfcs/3424-cargo-script.html#prio...
- ComputerGuru 3y agoThanks! > 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.
- FFFXXX 3y agoThats awesome, now I'm going to have to find a reason to use this for something ;) 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.
- ComputerGuru 3y agoNice catch! That warning definitely wasn't emitted the last time I tried this.. I don't know about using something like your suggestion which has actual side effects, but probably something like #![cfg_attr(invalid, allow(unused_attributes))] instead would work (without side effects).