mirror of
https://github.com/notohh/rustlings.git
synced 2024-12-18 06:58:10 -05:00
Simplify next_pending_exercise_ind
This commit is contained in:
parent
fc1f9f0124
commit
fd2bf9f6f6
1 changed files with 16 additions and 19 deletions
|
@ -313,25 +313,22 @@ impl AppState {
|
||||||
|
|
||||||
// Return the index of the next pending exercise or `None` if all exercises are done.
|
// Return the index of the next pending exercise or `None` if all exercises are done.
|
||||||
fn next_pending_exercise_ind(&self) -> Option<usize> {
|
fn next_pending_exercise_ind(&self) -> Option<usize> {
|
||||||
if self.current_exercise_ind + 1 == self.exercises.len() {
|
let next_ind = self.current_exercise_ind + 1;
|
||||||
// The last exercise is done.
|
self.exercises
|
||||||
// Search for exercises not done from the start.
|
// If the exercise done isn't the last, search for pending exercises after it.
|
||||||
return self.exercises[..self.current_exercise_ind]
|
.get(next_ind..)
|
||||||
.iter()
|
.and_then(|later_exercises| {
|
||||||
.position(|exercise| !exercise.done);
|
later_exercises
|
||||||
}
|
|
||||||
|
|
||||||
// The done exercise isn't the last one.
|
|
||||||
// Search for a pending exercise after the current one and then from the start.
|
|
||||||
match self.exercises[self.current_exercise_ind + 1..]
|
|
||||||
.iter()
|
.iter()
|
||||||
.position(|exercise| !exercise.done)
|
.position(|exercise| !exercise.done)
|
||||||
{
|
.map(|ind| next_ind + ind)
|
||||||
Some(ind) => Some(self.current_exercise_ind + 1 + ind),
|
})
|
||||||
None => self.exercises[..self.current_exercise_ind]
|
// Search from the start.
|
||||||
|
.or_else(|| {
|
||||||
|
self.exercises[..self.current_exercise_ind]
|
||||||
.iter()
|
.iter()
|
||||||
.position(|exercise| !exercise.done),
|
.position(|exercise| !exercise.done)
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Official exercises: Dump the solution file form the binary and return its path.
|
/// Official exercises: Dump the solution file form the binary and return its path.
|
||||||
|
|
Loading…
Reference in a new issue