News for hackers. Stuff that matters.
Re:Effective Rust by dangerlibrary on Thursday June 15, 2023 @12:14PM (#36342723)
Maybe my mind has been poisoned by a few years of Scala, but that just looks like normal code to me. Bundle up those errors and return a useful message to the user about why it failed, call it a day.
Re:Effective Rust by justinpombrio on Thursday June 15, 2023 @12:25PM (#36342887)
The point is that the code literally cannot panic under any circumstances. Just because there's a call to `unwrap()` or `[]` doesn't mean it's actually possible to trigger that panic. I made that more explicit the code, in case that wasn't clear.

If there's any question as to whether a line might actually panic or not, then absolutely, generate helpful error messages. But when you can tell through simple local reasoning that the panic will never happen, you can just use square brackets. It's OK.

Re:Effective Rust by dangerlibrary on Thursday June 15, 2023 @12:47PM (#36343248)
For the first time, I find myself wanting the equivalent of a :thumbs-up: reaction emoji on hn.
Re:Effective Rust by dllthomas on Thursday June 15, 2023 @02:36PM (#36344761)
> when you can tell through simple local reasoning that the panic will never happen

A problem is that you may be able to tell now, but as code changes and functions grow and split, that reasoning may grow less simple and less local without any changes of those lines in particular.

The odds of that, and the degree to which it matters, are of course both spectacularly situation dependent.

Re:Effective Rust by fireflash38 on Friday June 16, 2023 @09:41AM (#36356442)
And if my grandmother had wheels she'd be a bicycle!

The point being, take context into account. You can't code like everything everywhere is going to get modified to be worse.

Re:Effective Rust by dllthomas on Friday June 16, 2023 @10:27AM (#36357072)
I agree, I was just surfacing a sometimes missed piece of context.
Re:Effective Rust by defen on Thursday June 15, 2023 @12:22PM (#36342839)
The point is that the language has decided that some kinds of builtin operations can panic, in the interest of developer ergonomics. The alternative is that every indexing operation returns a Result, or you write 4 lines of code instead of 1 for every indexing operation. So the notion that the programmer should never write "panic!" in their code does not fully address the underlying problem, if the reason for not writing "panic!" is "library code should never panic".