Error handling without unwrap everywhere

Back to list2026-01-05
You

unwrap is fine in examples but what do real programs do?

ChatGPT

Propagate with ? and define one error type per crate boundary. thiserror for libraries, anyhow for applications is the common split: libraries need callers to match on variants, applications mostly need context and a good message.

You

I cannot mutate a vector while iterating over it.

ChatGPT

That is the rule doing its job: a resize would invalidate the iterator. Collect the changes and apply after, or iterate by index, or use retain when you are removing:

items.retain(|item| item.keep);

In C++ the same code compiles and reads freed memory occasionally, which is the difference being paid for.