How to Get a Hint for a Rustlings Exercise: CLI and Watch Mode Guide
Run rustlings hint [EXERCISE_NAME] in your terminal or press h while in rustlings watch mode to display the hint for any Rustlings exercise.
The rust-lang/rustlings repository provides an interactive learning environment for Rust where each exercise includes an embedded hint. Whether you prefer command-line tools or the interactive watch mode, retrieving these hints involves specific invocation patterns defined in the Rustlings source code.
Using the rustlings hint Command
The CLI hint sub-command provides the most direct way to view exercise hints when you are not running in watch mode. This command is defined in src/main.rs as part of the Subcommands enum.
Syntax and Usage
You can invoke the hint command with or without specifying an exercise name:
# Show hint for the next pending exercise
rustlings hint
# Show hint for a specific exercise (e.g., variables2)
rustlings hint variables2
When you omit the exercise name, Rustlings automatically selects the next pending exercise in the sequence and displays its associated hint.
How the CLI Hint Works
According to the source code in src/main.rs (lines 56-89), the Hint subcommand processes your request through the following steps:
-
Exercise Selection: If you provided a name, the system calls
app_state.set_current_exercise_by_name()to locate that specific exercise. Otherwise, it uses the current pending exercise. -
Hint Retrieval: The program accesses the
hintfield of theExercisestruct, which contains a&'static strreference to the hint text. -
Output: Finally, it prints the hint to stdout:
println!("{}", app_state.current_exercise().hint);
The hint text itself originates from each exercise's info.toml file, parsed by InfoFile::parse() and loaded into memory when Rustlings initializes.
Getting Hints in Watch Mode
When you run rustlings watch, the tool enters an interactive UI mode where you can toggle hints without leaving the exercise context. This functionality is implemented in src/watch/state.rs.
The Watch Mode Interface
To access a hint while watching:
-
Start watch mode:
rustlings watch -
When the interactive UI displays the current exercise, press
h.
The first press of h toggles the hint display on. Subsequent presses have no effect until you complete the exercise or reset the state.
Technical Implementation of Watch Mode Hints
The watch mode hint system relies on a boolean flag and event handling defined in src/watch/state.rs (lines 63-70 and 96-100).
When you press h, the input thread generates a WatchEvent::Input(InputEvent::Hint), which triggers WatchState::show_hint():
pub fn show_hint(&mut self, stdout: &mut StdoutLock) -> io::Result<()> {
if !self.show_hint {
self.show_hint = true;
self.render(stdout)?;
}
Ok(())
}
During the render phase (WatchState::render), the system checks the show_hint flag. If true, it displays the hint with cyan-colored "Hint" heading:
if self.show_hint {
stdout.queue(SetAttributes(HEADING_ATTRIBUTES))?
.queue(SetForegroundColor(Color::Cyan))?;
stdout.write_all(b"Hint")?;
// ...
stdout.write_all(self.app_state.current_exercise().hint.as_bytes())?;
}
The UI also dynamically hides the :hint key label once the hint is visible, only showing h:hint when show_hint is false.
Where Hints Are Stored
Every Rustlings exercise stores its metadata, including the hint text, in a TOML configuration file located at exercises/**/info.toml. For example, the hint for variables2 resides in exercises/variables2/info.toml:
hint = """
Remember that `let` creates an immutable binding unless you add `mut`.
"""
When Rustlings initializes, InfoFile::parse() reads these TOML files and populates the hint field of each Exercise struct defined in src/exercise.rs. This design allows the Rustlings maintainers to update hints without modifying the core application code.
Summary
- Use
rustlings hint [EXERCISE_NAME]to display hints directly from the command line when not in watch mode, as implemented insrc/main.rs. - Press
hin watch mode to toggle the hint display in the interactive UI, handled byWatchState::show_hint()insrc/watch/state.rs. - Hints are stored in each exercise's
info.tomlfile and loaded into theExercisestruct during initialization. - The hint system is idempotent in watch mode—once displayed, pressing
hagain has no effect until you progress to the next exercise.
Frequently Asked Questions
Can I see a hint without running rustlings?
Yes. Since Rustlings stores hints as plain text in exercises/**/info.toml files, you can read the hint directly by opening the corresponding info.toml file for any exercise. For example, cat exercises/functions/functions1/info.toml displays the hint for the functions1 exercise without invoking the Rustlings binary.
Why doesn't the hint show up when I press h multiple times?
The watch mode hint toggle is designed to be idempotent. According to the implementation in src/watch/state.rs, the show_hint() method checks if !self.show_hint before setting the flag to true and re-rendering. Once the flag is true, subsequent calls return immediately without changing the display. This prevents the UI from flickering or re-printing the hint unnecessarily while you work on the exercise.
How do I add custom hints to my own Rustlings fork?
To add or modify hints in a custom Rustlings fork, edit the hint field in the info.toml file located within each exercise's directory under exercises/. The hint value supports multi-line strings using TOML's triple-quote syntax ("""). After modifying the TOML files, rebuild the project if necessary. The InfoFile::parse() function in the source code automatically loads your custom hint text into the Exercise struct when Rustlings runs.
What's the difference between rustlings hint and watch mode hints?
The CLI rustlings hint command is a one-off operation that prints the hint to your terminal and exits, making it ideal for quick reference without entering the interactive environment. It is implemented in src/main.rs and directly accesses the Exercise.hint field. In contrast, watch mode hints are part of the persistent interactive UI (rustlings watch) implemented in src/watch/state.rs. They toggle within the live interface, display with cyan-colored formatting, and automatically hide the :hint key label once shown. Watch mode hints are designed for iterative learning while you edit code, whereas the CLI command suits checking hints before you begin coding.
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 →