From reading this thread, it looks like its possible to use the shebang to run Rust *.
#!/usr/bin/env rustc
fn main() {
println!("Hello World!");
}
Making this executable and running does compile, but not run the code.
chmod +x hello_world.rs
./hello_world.rs
However this only compiles the code into hello_world
.
Can *.rs
files be executed directly, similar to a shell script?
* This references rustx, I looked into this, but its a bash script which compiles the script every time (without caching) and never removes the file from the temp directory, although this could be improved. Also it has the significant limitation that it can't use crates.
There's
cargo-script
. That also lets you use dependencies.After installing
cargo-script
viacargo install cargo-script
, you can create your script file (hello.rs
) like this:To execute it, you need to:
To use crates from crates.io, please see the tutorial in the README linked above.