Rust/script
rust-scriptWhat rust-script Is
- rust-script is a lightweight tool that allows you to run single-file Rust programs as scripts—similar to how you might use Python, Ruby, or Node.js—without manually creating a full Cargo project.
- You can write Rust in a single
.rsfile with special "script headers," and run it directly with: - bash
rust-script my_script.rs - It handles:
Why People Use rust-script
Key Features
1. Single-file executables
2. Inline dependencies
- Add dependencies right in the file (much like Rust's
cargo-scriptused to allow): - rust
//! cargo-deps: reqwest = "0.11", tokio = { version = "1", features = ["full"] } tokiomain async fn main() { let body = reqwest::get("https://example.com").await.unwrap().text().await.unwrap(); println!("{body}"); } - rust-script reads these headers, generates a temporary Cargo project, compiles it, and caches it.
- Add dependencies right in the file (much like Rust's
3. Shebang support
4. Cargo-like experience, minimal ceremony
5. Fast re-runs via caching
How rust-script Compares to Alternatives
- rust-script: Easiest for single-file scripts with inline deps; very ergonomic.
- cargo-script: Similar predecessor; rust-script is generally more up-to-date and idiomatic.
- cargo +
fn main: Standard approach, but requires project scaffolding. - EvCxR (Jupyter/REPL): Great for interactive experimentation; slower for scripting.
- shebang with
cargo run: Possible but much heavier. - rust-script strikes the balance between real Rust and script-like workflows.
Installation
Execution model
Limitations
Quick Example
- If you'd like, I can also prepare: