this post was submitted on 22 Oct 2024
13 points (84.2% liked)

Rust

6071 readers
57 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

[email protected]

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 2 years ago
MODERATORS
 

I would like to share a bash script I made for when you want to simply run a rust script once and delete it. Instead of having compile the script with rustc, running the binary and then deleting the binary, you can achive all of this with this bash script below.

The first argument will be the rust script file name. The .rs file extension is optional. The rest of the arguments are passed into the executed binary.

Simply name the bash script to something like rust-run.sh.

#!/bin/bash

#Get file path from first parameter
path=$(dirname "$1")

#Get file name from first parameter
fileName=$(basename "$1")
fileName="${fileName%'.rs'}"

#Compile executable and save it in the same directory as the rust script
rustc "${path}/${fileName}.rs" -o "${path}/${fileName}"

#If rustc commands retuned any errors, unable to compile the rust script
if [ $? -ne 0 ]; then
    return
fi

#Execute compilled executable and pass the rest of the parameters into the executable
"${path}/${fileName}" ${*:2}

#Delete compillled executable
rm "${path}/${fileName}"

If someone wants to rewrite this in rust or add these features into the rustc, feel free to do so.

you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 1 points 1 month ago* (last edited 1 month ago) (1 children)

If it's useful to you, that is great.

Will rustc not just overwrite the old binary? If you are just doing a cleanup task, that's cool. If nuking the last binary is important, then just do it first:

rm ./code; rustc ./code.rs -o ./code; ./code -mah args

[โ€“] [email protected] 3 points 1 month ago

Or a makefile / justfile would be good too.

I put those on each directory and do just run to pick up the thing I was working on quickly.