Assert Macros का उपयोग
In this page:
assert!(condition);
assert_eq!(left, right);
assert_ne!(left, right);
assert!(condition, "message {}", value);
assert! for Boolean Conditions
assert!(condition) एक generic message के साथ panic करता है अगर दिया boolean expression false evaluate होता है, अन्यथा यह कुछ नहीं करता।
उदाहरण: assert! for Boolean Conditions
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `value`, set to `10`
let value = 10;
// Assert that value > 0 — panics if this is false
assert!(value > 0);
// Print "Assertion passed: value is positive" to the console (with a trailing newline)
println!("Assertion passed: value is positive");
}
Login to try C/C++/Java/PHP code in the editor
assert_eq! for Equality Checks
assert_eq!(left, right) panic करता है अगर दोनों values बराबर नहीं हैं, और महत्वपूर्ण रूप से panic message में दोनों values print करता है, जो plain assert! की तुलना में test failures debug करना कहीं आसान बनाता है।
उदाहरण: assert_eq! for Equality Checks
// Define the function `square` taking `n` and returning an integer
fn square(n: i32) -> i32 {
n * n
}
// Entry point: execution of the program starts here
fn main() {
// Assert that square(4), 16 — panics if this is false
assert_eq!(square(4), 16);
// Print "Assertion passed: square(4) == 16" to the console (with a trailing newline)
println!("Assertion passed: square(4) == 16");
}
Login to try C/C++/Java/PHP code in the editor
assert_ne! for Inequality Checks
assert_ne!(left, right) assert_eq! का उल्टा है, panic करते हुए अगर दोनों values बराबर निकलें जब वे अलग होने की उम्मीद थी।
उदाहरण: assert_ne! for Inequality Checks
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `original`, set to `5`
let original = 5;
// Declare an immutable variable `modified`, set to `original + 1`
let modified = original + 1;
assert_ne!(original, modified);
// Print "Assertion passed: values are different" to the console (with a trailing newline)
println!("Assertion passed: values are different");
}
Login to try C/C++/Java/PHP code in the editor
Adding a Custom Panic Message
तीनों assert macros main condition के बाद एक optional format string और arguments accept करते हैं, failure पर एक clearer, ज़्यादा specific panic message produce करते हुए।
उदाहरण: Adding a Custom Panic Message
// Entry point: execution of the program starts here
fn main() {
// Declare an immutable variable `count`, set to `3`
let count = 3;
// Assert that count > 0, "count must be positive, got {}", count — panics if this is false
assert!(count > 0, "count must be positive, got {}", count);
// Print "Assertion passed with count = {}" to the console, substituting in `count` (with a trailing newline)
println!("Assertion passed with count = {}", count);
}
Login to try C/C++/Java/PHP code in the editor
assert_eq!को उन values के साथ उपयोग करना जोDebugimplement नहीं करतीं, क्योंकि macro को failure पर दोनों sides print करने के लिएDebugचाहिए।assert!(एक single boolean जांचता है) कोassert_eq!/assert_ne!(दो values compare करता है) के साथ confuse करना।- यह भूल जाना कि assert macros tests के बाहर भी काम करते हैं, और अगर इनकी condition runtime पर fail हो जाए तो पूरे program को panic कराएंगे, सिर्फ
cargo testके दौरान नहीं।
assert!(condition)panic करता है अगर condition false है।assert_eq!(a, b)panic करता है अगरaऔरbबराबर नहीं हैं, आसान debugging के लिए दोनों values print करते हुए।assert_ne!(a, b)panic करता है अगरaऔरbबराबर हैं।- तीनों macros program में कहीं भी काम करते हैं, सिर्फ
#[test]functions के अंदर नहीं, और एक optional custom panic message accept करते हैं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: