Closed
Description
At present, the documentation for unreachable!()
has an example where you could not argue that the 'unreachable' code is indeed unreachable:
struct Item { weight: uint }
fn choose_weighted_item(v: &[Item]) -> Item {
assert!(!v.is_empty());
let mut so_far = 0u;
for item in v.iter() {
so_far += item.weight;
if so_far > 100 {
return *item;
}
}
// The above loop always returns, so we must hint to the
// type checker that it isn't possible to get down here
unreachable!();
}
I would argue a better example would see a loop more like:
fn get_at_lest_100(){
loop {
sum += some_function_that_returns_unsigned_numbers();
if sum > 100 { return sum; }
}
unreachable!();
}
Perhaps I am miss understanding the purpose of this macro, in the example I provided here, is the compiler able to determine that the loop will keep going until eventually the return sum;
is executed, and thus there is no need for the unreachable();
. In which case, I think the documentation should make it clear that in such situations the macro is not required.
I'd argue it is as important to know where not to use this macro as it is when to use it.
Metadata
Metadata
Assignees
Labels
No labels