Skip to content

Instantly share code, notes, and snippets.

@isurfer21
Created July 19, 2024 03:16
Show Gist options
  • Save isurfer21/421b5ca7b7116cfd31e7f3e7faba0211 to your computer and use it in GitHub Desktop.
Save isurfer21/421b5ca7b7116cfd31e7f3e7faba0211 to your computer and use it in GitHub Desktop.

Revisions

  1. isurfer21 created this gist Jul 19, 2024.
    42 changes: 42 additions & 0 deletions rust.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    #!/bin/bash

    HELP_MENU="Usage: ${0##*/} [-h|--help] <rust_filename>
    Compile and run a Rust file, then delete the binary.
    Options:
    -h, --help Show this help menu
    "

    if [ $# -eq 0 ]; then
    echo "Error: Rust filename is required"
    echo "$HELP_MENU"
    exit 1
    fi

    while [ $# -gt 0 ]; do
    case "$1" in
    -h|--help)
    echo "$HELP_MENU"
    exit 0
    ;;
    *)
    RUST_FILENAME="$1"
    ;;
    esac
    shift
    done

    if [ ! -f "$RUST_FILENAME" ]; then
    echo "Error: File '$RUST_FILENAME' does not exist"
    exit 1
    fi

    rustc "$RUST_FILENAME"
    if [ $? -ne 0 ]; then
    echo "Error: Compilation failed"
    exit 1
    fi

    BIN_FILENAME="${RUST_FILENAME%.rs}"
    ./"$BIN_FILENAME"
    rm "$BIN_FILENAME"