Menu

[Rust Guide] 9.4. When Should You Use Panic!
📰
0

[Rust Guide] 9.4. When Should You Use Panic!

DEV Community·SomeB1oody·about 1 month ago
#mVlVOA6e
Reading 0:00
15s threshold

If you find this helpful, please like, bookmark, and follow. To keep learning along, follow this series. 9.4.1 General Principles Chapter 9.1, “Unrecoverable Errors and panic! ”, already explained that Rust has two kinds of errors: recoverable and unrecoverable. Calling panic! is equivalent to an unrecoverable error. Returning a Result type means the error is propagated, and such an error is recoverable. If you think you can decide on behalf of the caller of your code that a situation is unrecoverable, then you can write panic! . If your function returns Result , you are effectively giving the caller of the code the right to decide how to handle the error. The caller can then decide whether to recover from it, or it can consider the error unrecoverable and call panic! itself. In short, if you are defining a function that may fail, prefer returning Result . If you believe a situation is definitely unrecoverable, use panic! . 9.4.2 Scenarios Where panic!…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More