You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As you can see, String::from_utf8 is an odd one out, returning a Result rather than an Option. I propose that all these functions return a Result, as this more closely matches the semantics of Option and Result. Option is generally used where Some() or None are both valid return values. Result is used where Ok() is a valid value and Err() means the function could not perform as desired. The latter more closely matches common use of from_utf8 (we expect this buffer to be a utf8 string, but need to confirm it before turning it into a str or String, and something has gone wrong if it's not utf8). Running through the rust compiler source code, it's almost exclusively used in this fashion: virtually all cases are followed immediately by a .unwrap(), panicking the task if the buffer was not utf-8.
Exactly what the Result's Err() variant should hold, I do not know. It seems that it should return a proper FromError-style enum with a variant like UtfDecodeError, but I do now know how many different error enums we want in the standard library. I leave it to someone else to decide what's most useful and elegant.
The text was updated successfully, but these errors were encountered:
RFC #236 has a strong opinion supporting for this change. Among other reasons, the must_use attribute on Result alone is enough to justify that.
(Aside: While Result is unanimously better for this issue in my opinion, we still don't have a good convention on functions that optionally consume its argument and Result is not desirable. Can't think of any good example, though, so it may be fine.)
The functions (note the return types):
As you can see, String::from_utf8 is an odd one out, returning a Result rather than an Option. I propose that all these functions return a Result, as this more closely matches the semantics of Option and Result. Option is generally used where Some() or None are both valid return values. Result is used where Ok() is a valid value and Err() means the function could not perform as desired. The latter more closely matches common use of from_utf8 (we expect this buffer to be a utf8 string, but need to confirm it before turning it into a str or String, and something has gone wrong if it's not utf8). Running through the rust compiler source code, it's almost exclusively used in this fashion: virtually all cases are followed immediately by a .unwrap(), panicking the task if the buffer was not utf-8.
Exactly what the Result's Err() variant should hold, I do not know. It seems that it should return a proper FromError-style enum with a variant like UtfDecodeError, but I do now know how many different error enums we want in the standard library. I leave it to someone else to decide what's most useful and elegant.
The text was updated successfully, but these errors were encountered: