What is the idiomatic way to return an error from a function with no result if successful?
Use fn do_work() -> Result<(), WorkError>
.
Result<(), WorkError>
means you want the work to be done, but it may fail.
Option<WorkError>
means you want to get an error, but it may be absent.
You probably want the work to be done but not to get an error when you write do_work()
, so Result<(), WorkError>
is the better choice.
I would expect Option<WorkError>
only be used in cases like fn get_last_work_error() -> Option<WorkError>
.
Rust is "pretty strongly typed" (and please, please don't call me out on how I measure how strongly typed a language is...). I mean this in the sense that Rust generally gives you the tools to let types "speak" for you and document your code, therefore it's idiomatic to use this feature to write readable code.
In other words, the question you're asking should be more "which type represents best what the function does to anyone who reads its signature?"
For Result<(), Workerror>
you can see straight from the docs
Result is a type that represents either success (Ok) or failure (Err)
So, specialized for your case, it means your function returns nothing if it's successful (represented by Ok<()>
) or WorkError
if there's an error (Err<WorkError>
). This is a very direct representation in code of the way you described the function in your question.
Compare this to Option<WorkError>
or Option<()>
Type Option represents an optional value: every Option is either Some and contains a value or None, and does not
In your case Option<WorkError>
would be saying to the reader "this function should return a WorkError
but it may return nothing". You can document that the "return nothing" case means that the function was actually successful, but that's not very evident from types alone.
Option<()>
says "this function can return nothing or have no meaningful return", which can be a reasonable thing to say if WorkError
contains no other info (like an error type or an error message) and it's practically only a way to say "an error has occurred". In this case a simple bool
carries the same information... Otherwise the Result
lets you return some more info associated with the error.