anyhow::Error is not cloneable

I never realized before that anyhow::Error is not cloneable or copyable. I need to accumulate an array of errors. Not as text; I need to take corrective action on them. Is there some way to do that?

In general you can solve this class of problem with Rc or Arc. Ideally for a compact and single-indirection representation you'd use Arc<dyn Error + Send + Sync> or Rc<dyn Error>, but if you need anyhow features, then Arc<anyhow::Error> works.

(Some concrete error types may themselves implement Clone, which is even better.)


Just as an aside, std::io::Error doesn't implement Clone, so unless you wrap errors as @kpreid suggests, you'll have this problem.

One advantage of boxing errors is that they take up less space in a Result.

Turned out that I just needed my own array initializer. I just needed to pre-fill a Vec of Option<Error> with None. (The array starts out all None and is updated as items with various indices are processed.)
That can be done, but not with vec!. vec! fills an array by making one value and duplicating it with .clone().

Initializing the array with
let mut errs: Vec::<Option<Error>> = (0..n).map(|_| None).collect();


See also.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.