logoalt Hacker News

dhermanlast Tuesday at 7:01 PM2 repliesview on HN

Ha, I just tried the same trick with Rust:

  //$HOME/.cargo/bin/rustc "$0" && ${0%.rs} "$@" ; exit
  
  use std::env;
  
  fn main() {
      println!("hello, world!");
      for arg in env::args() {
          println!("arg: {arg}");
      }
  }
Total hack, and it litters ./ with the generated executable. But cute.

Replies

eolpinlast Tuesday at 8:20 PM

Fortunately this hack isn't necessary for rust, shebangs are syntactically valid and single-file scripts can be executed with cargo nightly: https://doc.rust-lang.org/nightly/cargo/reference/unstable.h...

show 2 replies
timcavellast Tuesday at 7:43 PM

[dead]