There are at least 3 methods to stop program execution
panic!
assert!
(and it's all siblings)env::panic
How to properly stop smart contract execution?
Is there any preferred way? When someone should use env::panic
?
They all will eventually call env::panic
. From the docs Terminates the execution of the program with the UTF-8 encoded message.
. It is a wrapper around a host function imported to the contract.
As for the other two, assert!
checks a boolean and calls panic!
with a message.
They both support the fmt::Display trait, which means you can have string interpolation with "{}" marking where a passed string will go in the order of their appearance.
e.g.
assert!(b, "{}", "oops");
/// is
if (b) {
panic!("{}", "oops");
}
/// is equivalent to
if (b) {
env::panic(format!("{}", "oops"));
}
So you can use any one as you see fit. A great place to see examples of them in the near-sdk-rs/examples
like the fungible token contract.