Re:A half-hour to learn Rust
by brabel on Saturday January 02, 2021 @05:26AM
(#25611771)
Why use a Result whose error type is empty?
[ Parent
| on HN ]
Re:A half-hour to learn Rust
by CJefferson on Saturday January 02, 2021 @05:47AM
(#25611897)
I should have said :) I am writing backtracking algorithms (think something like a Sudoku solver where we fill in values by guessing), and an 'Error' is when we can deduce the Sudoku cannot be filled in, so we have to backtrack.
I find Rust's ? notation gives a very natural way of writing such algorithms. I don't care "why" filling in the Sudoku failed, particularly because the solver will typically fail millions of times a second, so I definately don't want constructing the error to be expensive in any way.
I would suggest Option is the more semantic type to use for your situation - None indicating no valid solution on this path
Re:A half-hour to learn Rust
by CJefferson on Saturday January 02, 2021 @10:23AM
(#25613385)
Oh, today I learned the ? operator works with Option as well as Result! I might try that out, thanks.
IIRC, it didn't work with Option in the past, so you might have been correct when your code was written. The ? operator came originally from the try!() macro, which was specific to Result; unlike the macro, it was designed to be extensible through the still-unstable std::ops::Try trait, but IIRC that trait was initially implemented only for Result, and only later was extended to Option and a couple of others.
Re:A half-hour to learn Rust
by fasterthanlime on Saturday January 02, 2021 @08:37PM
(#25618522)
It's sometimes called a type-level boolean. Instead of trying to remember what true or false means, you have explicit variants associated with success or failure. (The underlying representation, including bit width, is the same)