There was a discussion on reddit about this a few days ago.
I think that suffers the same “unsafe is wrong” problem: unsafe { ... } can be regarded as “I’ve taken on the compiler’s job to prove this correct”… which is something the callers of exact_size literally cannot do since they get called with arbitrary user-defined code. It’s up to the implementers to ensure they’re not feeding bad data back up into the use-sites.
Unfortunately that model doesn’t quite work: presumably the slice iter sets an exact size hint, in which case one can implement size_hint for any type as:
fn size_hint(&self) -> SizeHint {
[1, 2, 3].iter().size_hint()
}
with absolutely no connection between the return value and the real size, and no unsafe. A solution may be to make size_hint unsafe to call, essentially combining the two approaches.