Skip to content

Instantly share code, notes, and snippets.

@brendanzab
Last active February 28, 2022 20:57
Show Gist options
  • Save brendanzab/8253053 to your computer and use it in GitHub Desktop.
Save brendanzab/8253053 to your computer and use it in GitHub Desktop.

Prototyping Rust Code in CodeRunner

CodeRunner is a nifty little app for OSX that allows you to play around with test code with minimal fuss. Here are some instructions for setting it up to build Rust code.

CodeRunner

Setup instructions

  1. Go to CodeRunner > Preferences...
  2. Select the Languages tab
  3. Down the bottom of the list view, click the plus sign to add a new language called Rust
  4. Enable the Language uses compilation script checkbox, and click Edit script
  5. Copy the following code into compile.sh:
#!/bin/bash

compname=`echo "$1" | sed 's/\(.*\)\..*/\1/'`
rustc $3 "$1" -o "$compname"
status=$?
if [ $status -eq 0 ]; then
    echo $compname
    exit 0
elif [ $status -eq 127 ]; then
    echo -e "\nTo run code in this language, you need to install the Rust compiler from https://github.com/mozilla/rust/."
fi
exit $status
  1. In the Run Command textbox, enter ./$compiler.
  2. Add the following text to the Code Template:
fn main() {
    %@
}
  1. Set the Syntax Mode to None
  2. Enter rs for the File Extension
@perkinsb1024
Copy link

In CodeRunner 2, the names of the arguments have changed, so the code needs to be updated slightly. I've verified that a simple "Hello, World" program will compile and run in CodeRunner 2 by changing:
$1 to $CR_FILENAME
$3 to $@

So your compile.sh should look like this:

#!/bin/bash

compname=`echo "$CR_FILENAME" | sed 's/\(.*\)\..*/\1/'`
rustc $@ "$CR_FILENAME" -o "$compname"
status=$?
if [ $status -eq 0 ]; then
    echo $compname
    exit 0
elif [ $status -eq 127 ]; then
    echo -e "\nTo run code in this language, you need to install the Rust compiler from https://github.com/mozilla/rust/."
fi
exit $status

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment