I think what you might be looking for is something like Rustling. Here are several methods that can help you manage numerous example executable files better. You can choose to use them according to your own habits and needs.
Method 1: Use the "rust_study" folder combined with "cargo new" to create learning examples
- Create the main folder and project structure
First, create a folder named "rust_study". This folder will serve as the general directory where you store all your learning examples. After entering this folder, you can use the "cargo new" command to create each individual learning example project. For example, if you want to create an example project named "example_1", open the command line in the "rust_study" directory and execute the "cargo new example_1" command. This will generate a new Rust project structure, which is similar to the following:
rust_study
└── example_1
├── Cargo.toml
└── src
└── main.rs
Each project created by "cargo new" has its own independent "Cargo.toml" file for managing project dependencies and other information, as well as the "src/main.rs" file for writing example code. You can create multiple different example projects in this way, such as "example_2", "example_3", and so on, and put different types of examples in different projects respectively.
- Write and run example code
In the "src/main.rs" file of each project, you can write the corresponding example code. When you want to run a certain example, enter the corresponding project directory (for example, "rust_study/example_1"), and then execute the "cargo run" command in this directory. This will run the executable file in this project, that is, execute the code logic written in "src/main.rs". This way makes each example project independent of each other, with a clear structure, which is convenient for managing and viewing different example contents.
Method 2: Utilize the "bin" directory (with a slight adjustment based on the original project structure)
- Adjust the directory structure
In your existing project (assuming the root directory of your current project has a certain name, such as "rust_project"), you can create a folder named "bin" under the "src" directory, and then sort the numerous independent executable files you had before into different subfolders under the "bin" directory according to their types. For example:
rust_project
└── src
└── bin
├── type_1_examples
│ ├── example_1.rs
│ └── example_2.rs
└── type_2_examples
├── example_3.rs
└── example_4.rs
Here, different types of learning binarys files are placed in different subfolders such as "type_1_examples" and "type_2_examples", which is convenient for classified management.
- Modify the "Cargo.toml" file to specify executable files
Next, you need to specify the path of each executable file in the "Cargo.toml" file in the root directory of the project through the "[[bin]]" table, as shown in the following example:
[[bin]]
name = "001"
path = "src/example/type_1_examples/example_1.rs"
[[bin]]
name = "002"
path = "src/example/type_2_examples/example_3.rs"
In this way, configure the path and corresponding name of each ".rs" file that you want to be an executable file in the "Cargo.toml" file. After that, you can run the corresponding example file by using the command like "cargo run --bin 001" (replace "001" here with the actual file name you configured). This method can also achieve effective management of numerous examples by reasonably organizing the directory and configuring it on the basis of the original project.
Thank you danjl1100 for correcting the mistake
Method 3: Use a workspace
- Create a workspace structure
Create a new folder as the root directory of the workspace, such as "rust_workspace", and then create different sub-project (package) directories and corresponding "Cargo.toml" files in it to store different types of example contents. The structure is probably as follows:
rust_workspace
├── Cargo.toml (The main configuration file of the workspace)
├── example_package_1
│ ├── Cargo.toml
│ └── src
│ └── main.rs (Write the example code of the first type here)
└── example_package_2
├── Cargo.toml
└── src
└── main.rs (Write the example code of the second type here)
- Configure the main "Cargo.toml" file of the workspace
In the "rust_workspace/Cargo.toml" file, configure the packages included in the workspace, like this:
[workspace]
members = ["example_package_1", "example_package_2"]
-
Configure the "Cargo.toml" files of each sub-project and write code
In the "Cargo.toml" file of each sub-project, you can configure project information, dependencies, and other contents as in a regular project, and then write specific example code in the corresponding "src/main.rs" file. For example, write a certain type of example in "example_package_1/src/main.rs" and another type of example in "example_package_2/src/main.rs".
-
Run and manage example projects
To run the example in a certain sub-project, just execute the command like "cargo run -p example_package_1" (replace the specific sub-project name after "-p" according to the actual situation) in the root directory of the workspace. The advantage of using a workspace is that it can manage different types of examples in a more modular way. The dependencies of each sub-project can also be relatively independent and shareable, which is convenient for you to continuously expand and organize the learning example contents later.
I hope the above methods can help you manage the numerous example executable files you created during your Rust learning process better. You can choose the appropriate method according to your own usage habits.