Skip to content
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

Rewrite mentions of try! macro to use ? operator #92

Merged
merged 1 commit into from Feb 23, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions idioms/dtor-finally.md
Expand Up @@ -23,8 +23,8 @@ fn bar() -> Result<(), ()> {

// The dtor of _exit will run however the function `bar` is exited.
let _exit = Foo;
// Implicit return in try!.
try!(baz());
// Implicit return with `?` operator.
baz()?;
// Normal return.
Ok(())
}
Expand All @@ -35,11 +35,11 @@ fn bar() -> Result<(), ()> {

If a function has multiple return points, then executing code on exit becomes
difficult and repetitive (and thus bug-prone). This is especially the case where
return is implicit due to a macro. A common case is `try!` which returns if the
result is an `Err`, but continues if it is `Ok`. `try!` is used as an exception
handling mechanism, but unlike Java (which has `finally`), there is no way to
schedule code to run in both the normal and exceptional cases. Panicking will
also exit a function early.
return is implicit due to a macro. A common case is the `?` operator which
returns if the result is an `Err`, but continues if it is `Ok`. `?` is used as
an exception handling mechanism, but unlike Java (which has `finally`), there is
no way to schedule code to run in both the normal and exceptional cases.
Panicking will also exit a function early.


## Advantages
Expand Down