diff --git a/README.md b/README.md index f72507a..1fcee53 100644 --- a/README.md +++ b/README.md @@ -25,3 +25,18 @@ You can find the slides [here](./slides/slides.md) * [Rustlings](https://rustlings.rust-lang.org/) * [Rustinfinity](https://www.rustfinity.com/) * [The Rust Book Experiment](https://rust-book.cs.brown.edu/) + +## Running the files + +Before attempting to run the Rust files in this repo, ensure that you already [installed Rust for your operating system](https://www.rust-lang.org/tools/install) and have at least `cargo` or `rustc` available on your path (system or user). + +To run the files in this course, navigate to the directory of the particular lesson and run the `cargo run` command. An example is shown below: + +```sh +cd day-1/rust-basics +cargo run +``` + +If you wish to build a binary which you can run at a later time, you can use the `cargo build --release` command for an optimized release or the `cargo build` for an unoptimized release (debug release). + +If you'd rather not navigate into directories to run files, you can utilize the `runfiles.sh` script on the root directory. It gives you an overview of all the `main.rs` files in the course and allows you run them iteractively. It uses `rustc` which Cargo also uses under the hood. diff --git a/runfiles.sh b/runfiles.sh new file mode 100644 index 0000000..07a8c15 --- /dev/null +++ b/runfiles.sh @@ -0,0 +1,37 @@ +#!/bin/bash + +function rust_run(){ + filepath=$(mktemp /tmp/mainXXXXXX) + rustc "$1" -o "$filepath" + "$filepath" + rm "$filepath" +} + + +shopt -s globstar + +declare -A file_paths + +echo "Available files" +rust_files=(day*/**/main.rs) +for index in "${!rust_files[@]}"; do + echo "$((index+1)). ${rust_files[$index]}" +done + + +echo -e "\n" + +max_index=${#rust_files[@]} + +while true; do + read -p "Which file do you want to execute (1 - $max_index): " file_index_to_execute + + if [[ $file_index_to_execute =~ ^[0-9]+$ ]] && (( file_index_to_execute >= 1 && file_index_to_execute <= max_index )); then + rust_run "${rust_files[$((file_index_to_execute - 1))]}" + break + else + echo "Invalid input. Please enter a number between 1 and $max_index." + fi +done + +