Rust debugging exercises
Rust guarantees memory safety at compile time, but logical defects and runtime panics still occur: calling .unwrap() on a None Option crashes in production, slicing a String by raw byte indices splits multi-byte UTF-8 characters, and double-borrowing a RefCell panics dynamically.
These exercises train you to fix real Rust bugs: safe error handling with ? and match, UTF-8 character indexing, channel synchronization, and iterator exhaustion.
Practise by topic
How to debug a Rust function
- Read the panic message. Rust panics give precise failure explanations, like 'called Option::unwrap() on a None value' or 'byte index is not a char boundary'.
- Avoid raw unwrap() in production logic. Replace unwrap() with unwrap_or, unwrap_or_else, or pattern matching.
- Respect UTF-8 boundaries. In Rust, String indexing is by byte, not character. Use .chars() or .char_indices() to handle unicode safely.
- Check integer overflow. In debug mode, u8 / i32 additions panic on overflow. Use saturating_add or checked_add when bounds are unpredictable.
- Audit RefCell borrow scopes. Ensure RefCell::borrow_mut guards are dropped before attempting a second borrow in the same execution path.
The exercises
Easy
- Unchecked integer addition overflow panic on u8Logic
- Unwrap on None panic from HashMap lookupNull and undefined
- Off-by-one inclusive range panic in slice windowOff-by-one
- Empty slice first element unwrap panicNull and undefined
- Rust f64 comparison without epsilon delta toleranceLogic
- Unnecessary string clone when borrowing sufficesLogic
- Vector capacity pre-allocation vs length indexingLogic
Medium
- Unexpected runtime panic during file processingNull and undefined
- Iterator Error in Vector ProcessingScope and closure
- String slice panics on non-UTF-8 char boundaryMutation and copying
- Array Indexing Error in Iterator Sum CalculationLogic
- Panic on accessing nested RefCell dataScope and closure
- Unexpected empty output from a Rust function using.map()Logic
- Recursive Trait Default Causes Stack OverflowLogic
- Unexpected output in Rust function due to variable shadowingScope and closure
- Immutable reference error during vector mutationScope and closure
- Indexing fails on preallocated but empty Rust vectorLogic
- Persistent memory usage despite object deletion in RustLogic
- Unexpected behavior in integer addition with large valuesLogic
- Struct usage error after partial move in RustScope and closure
- Unexpected file descriptor leak in Rust applicationLogic
- Async function causes MutexGuard error during awaitAsync and race conditions
- Variable inaccessible after closure use in RustScope and closure
- Unexpected Panic When Slicing a String in RustLogic
- Uninitialized memory access in array processingLogic
- Inconsistent ordering and equality in custom typesLogic
- Channel receiver try_recv() error handling issueLogic
- Unexpected behavior when converting structs in RustType
- String modification fails with Cow always borrowing issueScope and closure
- Segmentation fault during linked list traversalNull and undefined
- UTF-8 byte index is not a char boundary panicLogic
- Double mutable borrow panic with RefCellScope and closure
- Eager evaluation in Option unwrap_or fallbackLogic
- Bitwise shift count exceeds integer bit widthLogic
- Truncate string on non-char boundary panicsLogic
- Thread join omitted before parent process returnsAsync and race conditions
- Variable shadowing in match arm pattern bindingScope and closure
- Cursor position at EOF returns zero bytesLogic
- Downcast on Box dyn Any fails with wrong typeType
- Borrow checker vector mutation during element iterationScope and closure
- Regex compilation overhead in repetitive loopLogic
- Partial move from struct prevents usageScope and closure
- Cow always clones to owned unnecessarilyLogic
Hard
- Deadlock in Nested Mutex Locks During Resource AccessAsync and race conditions
- Clone Arc Mutex inside thread spawning loopAsync and race conditions
- Poisoned mutex lock propagation after panicAsync and race conditions
- Channel receiver hangs due to retained sender cloneAsync and race conditions
- Unbounded channel memory leak under fast producerLogic
- Rc cycle prevents memory deallocationScope and closure
What you will practise
Questions
Are these compiler errors or runtime bugs?
BugHunt focuses on logic errors, runtime panics, and edge cases where code compiles but produces incorrect behavior.
Do I need cargo installed?
No. Test verification runs through BugHunt with instant feedback in your browser.