The finally in Kotlin, defer in Swift and .. in Rust?

Sometimes you need to execute several statements before leaving a block regardless how you leave it, normally or upon '?' Err condition. Is there in Rust something helping to do so?

Idiomatic Rust code doesn't panic, it relies on the use of the Result type for error handling.

That being said, you don't need a language construct such as try-catch-finally blocks in Rust, because Rust doesn't have the concept of exceptions.

If you don't want early returns... just don't return early! That is, don't use the ? operator. It cannot get any simpler than that.

3 Likes
6 Likes

The stackoverflow shows also couple solutions like that. It's some food to digest.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.