Right. Optimizations are usually couched in "as-if" language. The collect
induces a reallocation which does not have a forced link to the old capacity, so that is "observable behavior" as far as the optimizer is concerned.
AFAIU, Clone
can only be optimized away if it is also Copy
(though I'm not sure if this is actually happening). See these threads:
- Assuming `Clone::clone()` is equivalent to `Copy` semantics for all instances of `Copy`
- Optimized Clone of Copy Types?
There was a more recent one mentioning it too, but I'm not seeing it right now.