← Back to Rust Course | Chapter 15: Cargo, Testing & Best Practices | Lesson 4 of 8

Assert Macros का उपयोग

Assert macros आपके code में छोटे tripwires हैं जो सब कुछ रोक देते हैं और ज़ोर से शिकायत करते हैं अगर कुछ true नहीं है।
Syntax
rust
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

markup
// 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");
}

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

markup
// 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");
}

assert_ne! for Inequality Checks

assert_ne!(left, right) assert_eq! का उल्टा है, panic करते हुए अगर दोनों values बराबर निकलें जब वे अलग होने की उम्मीद थी।

उदाहरण: assert_ne! for Inequality Checks

markup
// 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");
}

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

markup
// 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);
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. assert_eq! को उन values के साथ उपयोग करना जो Debug implement नहीं करतीं, क्योंकि macro को failure पर दोनों sides print करने के लिए Debug चाहिए।
  2. assert! (एक single boolean जांचता है) को assert_eq!/assert_ne! (दो values compare करता है) के साथ confuse करना।
  3. यह भूल जाना कि 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.