Returning an error/option from Clone::clone

If it fails, it can't be an implementation of Clone. You can either add a separate function/method with whatever signature you like, or introduce a new trait, if such fallible cloning is common for your types.

Look at this from the perspective of a generic function implementation. You expect Clone, but someone sneaks in a fallible impl (assuming it were allowed). How do you deal with it? You can't. You don't expect it, there is nothing you could do but crash, and even that can't be properly expressed in code.

That said, it may be the case that Clone is indeed too restrictive, and ends up changed or deprecated in favour of a different trait. The fallible cloning problem is common if you allow fallible memory allocation in the language. Most clonable but not Copy types own some allocated memory, and would have to reacquire it on clone, which could fail if allocation were fallible. Currently an allocation failure in Rust results either in panic or the process killed by the OS.

2 Likes