-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
{Result,Option}::expect()
hurt the readability of Rust code
#35083
Comments
Renaming |
I agree with this as well. What about using |
Actually I think it means literally what "expect" means: regard sth. as likely to happen. Notice that it does not necessarily mean the subject "hopes", or "wishes" sth to happen subjectively. As in this code piece, it means the author of the code is expecting something to go wrong and to cause a panic, therefore s/he intentionally leaves a message explaining what the expected error is. |
I really think that expect should have been called something like Would not be a breaking change, just create a new alias and add a deprecation warning for self.get(key).expect("no entry found for key") would then become self.get(key).or_panic("no entry found for key") which is far more logical and readable, in my opinion. |
"expect" clashes with every language I can think of... python has "except" and "raise", rx libraries have .catchException(), other languages have try/catch/throw. Javascript promises have .then().catch(). The method "expect" feels like a rite of passage into Rust because you must know the history to understand the name choice. Clarity and conciseness should trump Rust specific historic motivations. |
It seems this issue involves people proposing two things:
Now, regarding the latter:
Regarding the former:
So it seems this has been resolved to the extent possible. Closing! |
Some bad smell code samples in tree:
in RawVec::with_capacity():
It maybe read as the author expect that capacity overflows.
in BTreeMap Index impl:
It maybe read as the author expect that no entry found for key.
in Rc tests:
It maybe read as the author expect ...(something)... failed?
in MIR:
It maybe read as the author expect an invalid terminator state?
Now in Rust community,
expect
is widely used. There are too many samples to list all of them here.These code are quite counter intuitive, they hurt the readability of Rust code.
The root source of all confusing come from the meaning of the 'msg' arg of
expect()
.Currently, it is interpreted as 'the error message on unexpected state'.
If we could change it to 'the message expounds expected behavior', that would be great.
And that will improve the readability significantly:
(The implementation of expect() requires to be changed slightly.)
But, is this a BREAKING-CHANGE?
Yes, and No.
The meaning of an arg of a stable method changed significantly, this IS a breaking-change. But the code logic don't change, program's behavior is not touched. What really changes is just the panic message.
If we do this change, all
expect()
caller side code should be reviewed carefully, almost all msg arg strings should be edited. Now i must admit honestly, that is a BIG breaking-change!If we treat this as a soundness issue like RFC 1214, we can and we should fix it, whatever it's breaking-change or not.
Maybe the easiest way is create another
expect2()
(or any creative name), and deprecated the currentexpect()
?What do you think about this, Rustaceans?
The text was updated successfully, but these errors were encountered: