How to Run a Specific Rustlings Exercise by Name: Complete Guide
Use rustlings run <exercise_name> to execute a single exercise directly from the command line, where <exercise_name> is the base filename without the .rs extension.
The Rustlings project provides a hands-on way to learn Rust through small, focused exercises. When you need to test a specific concept or debug a particular file, running individual exercises by name is more efficient than progressing through the entire sequence. This guide explains the exact command syntax and the underlying implementation in the rust-lang/rustlings repository.
The Quick Command to Run a Specific Rustlings Exercise
To run a specific exercise, use the run sub-command followed by the exercise identifier:
rustlings run variables
The <exercise_name> argument must match the base filename of the exercise located in the exercises/ directory. Do not include the .rs extension or any directory path prefixes. For example, to run exercises/05_vectors.rs, you would execute:
rustlings run 05_vectors
If you omit the exercise name entirely, Rustlings defaults to running the next pending exercise in the sequence:
rustlings run
How the Run Command Works Under the Hood
The "run by name" functionality is implemented across three core modules in the Rustlings codebase.
CLI Parsing in main.rs
The command-line interface defines the Run variant in src/main.rs (lines 44-48) with an optional name field:
/// Run a single exercise. Runs the next pending exercise if the exercise name is not specified
Run {
/// The name of the exercise
name: Option<String>,
},
When the user invokes rustlings run, the parser captures the optional exercise name and passes it to the application state handler.
Exercise Selection in app_state.rs
The set_current_exercise_by_name method in src/app_state.rs (lines 66-77) performs the lookup and validation:
pub fn set_current_exercise_by_name(&mut self, name: &str) -> Result<()> {
self.current_exercise_ind = self.exercises
.iter()
.position(|exercise| exercise.name == name)
.with_context(|| format!("No exercise found for '{name}'!"))?;
self.write()
}
This function iterates through the loaded Exercise list, finds the index where exercise.name matches the input string, updates current_exercise_ind, and persists the state to disk. If no match is found, it returns the error message: No exercise found for '...'!
Execution in run.rs
Once the exercise is selected, control passes to src/run.rs, where the run function constructs a RunnableExercise and executes it via the CmdRunner:
return run::run(&mut app_state);
The execution engine compiles the exercise file using the stored cargo command and reports success or failure based on the compilation and test results.
Practical Examples and Usage Patterns
Here are common scenarios for running specific Rustlings exercises:
| Command | Result |
|---|---|
rustlings run variables |
Compiles and runs exercises/variables.rs |
rustlings run 05_vectors |
Handles exercises with numeric prefixes |
rustlings run |
Runs the next pending exercise in sequence |
rustlings run --help |
Displays help for the run sub-command |
When working in watch mode, you can manually re-run the current exercise after editing by pressing r (if using the interactive watch interface) or by re-invoking the specific run command.
Common Pitfalls When Running Specific Exercises
To successfully run a specific Rustlings exercise by name, avoid these common mistakes:
-
Do not include the
.rsextension: The matcher looks for the base name only. Usingrustlings run variables.rswill fail with "No exercise found for 'variables.rs'!" -
Do not include directory paths: Even if the file is located at
exercises/03_if/if1.rs, you must runrustlings run if1, notrustlings run 03_if/if1. -
Match the name exactly: The comparison is case-sensitive and exact.
rustlings run Variableswill not matchvariables.rs.
Summary
- Use
rustlings run <exercise_name>to execute a specific exercise by its base filename (without.rs). - The command is implemented in
src/main.rs, with exercise lookup handled byset_current_exercise_by_nameinsrc/app_state.rs. - Omitting the name runs the next pending exercise automatically.
- The exercise name must match exactly, without file extensions or directory prefixes.
Frequently Asked Questions
What happens if I don't specify an exercise name with rustlings run?
If you execute rustlings run without providing an exercise name, Rustlings automatically runs the next pending exercise in the sequence. This is the default behavior implemented in the CLI parser, allowing you to progress through the curriculum without manually specifying each file.
Can I run a Rustlings exercise using its full file path?
No, you cannot use the full file path or the .rs extension when running a specific exercise. The set_current_exercise_by_name function in src/app_state.rs performs an exact string match against the exercise's base name field. You must provide only the filename without extension, such as variables instead of exercises/variables.rs.
Why does Rustlings say "No exercise found for '...'" when I try to run?
This error message is generated by the set_current_exercise_by_name method in src/app_state.rs when the provided name does not match any exercise in the loaded list. Common causes include including the .rs extension, using incorrect capitalization, adding directory prefixes, or simply misspelling the exercise name. The match must be exact and case-sensitive.
How do I run the next pending exercise automatically?
Simply execute rustlings run without any arguments. The CLI parser in src/main.rs defines the name field as an Option<String>, and when it is None, the program proceeds to run the next pending exercise in the sequence rather than looking up a specific name.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →