← Back to Rust Course | Chapter 1: Setup & Basics | Lesson 6 of 7

Comments लिखना

Comments आपके code में छोड़े छोटे notes हैं जिन्हें computer नज़रअंदाज़ करता है लेकिन लोग यह समझने के लिए पढ़ सकते हैं कि क्या चल रहा है।
Syntax
rust
// line comment
/* block comment */
/// doc comment for the next item
//! doc comment for the enclosing item

Line Comments

एक double slash // एक comment शुरू करता है जो उस line के आखिर तक जारी रहता है। // के बाद उस line पर जो कुछ भी है उसे compiler नज़रअंदाज़ करता है और यह विशुद्ध रूप से human readers के लिए मौजूद है।

उदाहरण: Line Comments

markup
// This program greets the user
fn main() {
    println!("Hello!"); // print a greeting
}

Block Comments

एक block comment /* से शुरू होता है और */ पर खत्म होता है, और कई lines तक span कर सकता है। यह code के एक हिस्से को temporarily disable करने या लंबे explanations लिखने के लिए उपयोगी है।

उदाहरण: Block Comments

markup
/*
   This program demonstrates block comments.
   Everything in here is ignored by the compiler.
*/
fn main() {
    println!("Block comments can span multiple lines");
}

Documentation Comments

किसी function या item के ठीक ऊपर रखे Triple-slash /// comments documentation comments हैं। cargo doc जैसे tools इन्हें पढ़ते हैं ताकि आपके code के लिए अपने आप HTML documentation generate कर सकें।

उदाहरण: Documentation Comments

markup
/// Prints a friendly greeting to standard output.
fn greet() {
    println!("Hello from a documented function!");
}

fn main() {
    greet();
}

Comments Do Not Affect Execution

आप comments में चाहे कितना भी text डालें, compiled program बिल्कुल एक जैसा behave करता है क्योंकि compiler compilation शुरू होने से पहले ही comments strip कर देता है। यह comments को clarity के लिए पूरी तरह free बनाता है।

उदाहरण: Comments Do Not Affect Execution

markup
fn main() {
    let x = 5; // this comment does nothing to the value
    println!("x is {}", x);
}
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. यह मानना कि comments execute होते हैं या program के behavior को किसी भी तरह प्रभावित करते हैं -- compiler इन्हें पूरी तरह skip कर देता है।
  2. comments का उपयोग यह explain करने के लिए करना कि obvious code *क्या* करता है बजाय इसके कि कोई non-obvious decision *क्यों* लिया गया।
  3. यह भूल जाना कि // सिर्फ line के आखिर तक comment करता है, इसलिए उसी line पर इसके बाद बचा code comment-out नहीं होता।
चैप्टर सारांश
  • // एक line comment शुरू करता है जो line के आखिर तक चलता है।
  • /* ... */ एक block comment शुरू करता है जो कई lines तक span कर सकता है।
  • किसी item से पहले /// एक documentation comment बनाता है जो cargo doc जैसे tools उपयोग करते हैं।
  • Comments को compiler नज़रअंदाज़ करता है और compiled program पर इनका शून्य effect होता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 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.