Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It was weird for me at first, but actually Rust provides a great solution here.

With https://github.com/DanielKeep/cargo-script you can have single file Rust "scripts" with ecosystem dependencies.

When executing the file (via the cargo-script wrapper), dependencies are fetched and a binary is compiled on demand (and cached for re-runs).

It's pretty awesome for scripts where type safety is important.

    #!/usr/bin/env run-cargo-script
    //! This is a regular crate doc comment, but it also contains a partial
    //! Cargo manifest.  Note the use of a *fenced* code block, and the
    //! `cargo` "language".
    //!
    //! ```cargo
    //! [dependencies]
    //! time = "0.1.25"
    //! ```
    extern crate time;
    fn main() {
        println!("{}", time::now().rfc822z());
    }
Then you can just execute it with ./my-script, which will download dependencies, compile and execute.


Likewise with Apache Groovy, which has optional typing:

    #!/usr/bin/env groovy
    // @Grab annotation won't work in scripts
    import static groovy.grape.Grape.grab
    grab(group: 'org.apache.commons', module: 'commons-csv', version: '1.5')
    
    println "Starting to parse CSV"



Thanks for sharing that, I had no idea. You just improved my day.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: