home ~ projects ~ socials

Run A Single File As A Script With Rust

#!/usr/bin/env cargo +nightly -Zscript

```cargo
[dependencies]
rand = "0.8.5"
```

use rand::prelude::*;

fn main() {
    let mut rng = rand::thread_rng();
    let num: u8 = rng.gen();
    println!("Random number: {}", num); 
}
Output:
Random number: 230

Notes

  • The original version of the code had to have the code block lines prepended with //!.
  • I'm removing those unnecessary //! comments from my site as I run across them
-- end of line --

References