Skip to content

Instantly share code, notes, and snippets.

@Phaqui
Created April 3, 2024 12:30
Show Gist options
  • Save Phaqui/5af0a873bb9a72b01bd5f6d1cad6513d to your computer and use it in GitHub Desktop.
Save Phaqui/5af0a873bb9a72b01bd5f6d1cad6513d to your computer and use it in GitHub Desktop.
Rust as script
Run a rust file as a script!
1: Install nightly
$ rustup install nightly
2: Set the script as executable
$ chmod +x script.rs
3: Profit!
$ ./script.rs
John Red Pants
John Red Shorts
John Red Sweater
John Red Hat
John Green Pants
[...abbreviated...]
Dependencies are listed as in Cargo.toml.
The [profile.dev] opt-level = 3 is to run in with optimizations, it can be omitted.
#!/usr/bin/env -S cargo +nightly -Zscript
```cargo
[package]
edition = "2021"
[profile.dev]
opt-level = 3
[dependencies]
itertools = "0.11"
```
use itertools::Itertools;
fn main() {
let names = vec!["John", "Kirsten", "Bobby", "Ramona", "Peter"];
let colors = vec!["Red", "Green", "Blue", "Yellow", "Purple"];
let clothes = vec!["Pants", "Shorts", "Sweater", "Hat"];
let s = vec![names, colors, clothes]
.iter()
.multi_cartesian_product()
.map(|tv| tv.iter().join(" "))
.join("\n");
println!("{s}");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment