Hacker News new | ask | show | jobs
by josephg 149 days ago
> why a division operator, or an array access, doesn't return a `Result` type in nice languages such as Rust?

Rust has standard library functions to do this, if you want. arr.get(index) returns an Option. Integer types have .checked_div for panic-free divide. Float already doesn’t panic on an invalid division - it just returns NaN or Infinity.

1 comments

That's great! Though, by making it not forced and giving users a choice, you never know which library code you call might not use those features and still panic when you use it.

(Just to be clear, I don't really propose that a language should offer only panic-free operations; I just think it's a nice thought experiment and discussion to have).

True. I'd really love rust to have a nopanic annotation you can put on function calls which guarantees that nothing in the call tree can panic.