this post was submitted on 22 Oct 2024
3 points (66.7% liked)

Rust

5893 readers
68 users here now

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

Wormhole

!performance@programming.dev

Credits

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

founded 1 year 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.

top 6 comments
sorted by: hot top controversial new old
[–] anti_antidote@lemmy.zip 1 points 1 hour ago

Is there a reason you're not just doing "cargo run"?

[–] nous@programming.dev 6 points 5 hours ago (1 children)

You might want to checkout rust-script which essentially does the same but also lets you use it as a hashbang line to make executable rust scripts. And works with cargo/can install deps.

There is also an RFC for this feature.

[–] trymeout@lemmy.world 0 points 4 hours ago

Thank for sharing rust-script which is basically what I made but written in Rust and seems to store the binary in another directory when the binary is exexuted.

Is the RFC trying to introduce a command cargo script to run single rust scripts?

[–] remotelove@lemmy.ca 2 points 6 hours ago (1 children)

I admire the willingness to share your work, but this is easier to do with a disposable one-liner at the prompt that you can repeat with an up arrow and a carriage return, if needed.

Sure, this script would be perfect for something like a cron job, but that would raise quite a few more questions as to why you would complie on a fixed schedule.

I can think of a few edge-cases where this script would be useful, but it just seems like it adds extra steps where extra steps might not be needed.

[–] trymeout@lemmy.world 0 points 5 hours ago (1 children)

It could be done with a one liner in the terminal but adding arguments when running the binary will be in the middle of the command, not at the end of the command.

Also the usecase for me is for sample scripts I have. This makes it easy to compile, run and delete the binary.

[–] remotelove@lemmy.ca 1 points 5 hours ago* (last edited 5 hours ago)

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