Run
Cmd
$ cargo script file.rs
OR add alias:
$ alias runrs='cargo script'
$ runrs file.rs
Script
$ cat file.rs
#!/usr/bin/env run-cargo-script
fn main() {
println!("Hello World!");
}
$ chmod +x file.rs
$ ./file.rs
Cargo dependency
$ cat file.rs
#!/usr/bin/env run-cargo-script
//! ```cargo
//! [dependencies]
//! time = "0.1.25"
//! ```
extern crate time;
fn main() {
println!("{}", time::now().rfc822z());
}
OR
$ cat file.rs
#!/usr/bin/env run-cargo-script
// cargo-deps: time="0.1.25"
extern crate time;
fn main() {
println!("{}", time::now().rfc822z());
}
Alternatives
scriptisto
h
A language-agnostic “shebang interpreter” that enables you to write scripts in compiled languages.
rust-script
h
Run Rust files and expressions as scripts without any setup or compilation step.