Call a function from file based on command line arguments dynamically in rust

55 Views Asked by At

This is my project structure

project/
├── Cargo.toml
├── src/
│   ├── main.rs
│   └── problems/
│       ├── file1.rs
│       ├── file2.rs
│       ├── ...
│       └── fileN.rs

Each of the fileN.rs files contain the same solve() function in it.

What I am trying to do is:

when I give command line arguments like cargo run -- 1, then the main.rs file should run problems::file1::solve(); // have some info to print

when I give command line arguments like cargo run -- 2, then the main.rs file should run problems::file2::solve(); // have some info to print

I tried using match statement with all the input numbers and these numbers pointing to the respective functions. but as the number of files in problems folder increases, the code becomes lengthy and unnecessary repetitive.

Is there a way to dynamically do this?

0

There are 0 best solutions below