panic! 和assert!
- panic!
- assert!(): if the part inside () is not true, the program will panic.
- assert_eq!(): the two items inside () must be equal.
- assert_ne!(): the two items inside () must not be equal. (ne means not equal)
fn main() {
let my_name = "Loki Laufeyson";
assert!(
my_name == "Loki Laufeyson",
"{} should be Loki Laufeyson",
my_name
);
assert_eq!(
my_name, "Loki Laufeyson",
"{} and Loki Laufeyson should be equal",
my_name
);
assert_ne!(
my_name, "Mithridates",
"You entered {}. Input must not equal Mithridates",
my_name
);
}