Error Handling with Result and Option (page 33)

This commit is contained in:
2026-02-05 11:20:54 +01:00
parent 575489e3f7
commit 2ed1aa2e00

View File

@@ -95,6 +95,16 @@ impl TodoList {
self.next_id += 1;
Ok(())
}
fn complete_task(&mut self, id: usize) -> Result<(), TodoError> {
match self.tasks.iter_mut().find(|task| task.id == id) {
Some(task) => {
task.completed = true;
Ok(())
}
None => Err(TodoError::TaskNotFound(id)),
}
}
}
fn main() {