Comments लिखना
// 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
// This program greets the user
fn main() {
println!("Hello!"); // print a greeting
}
Login to try C/C++/Java/PHP code in the editor
Block Comments
एक block comment /* से शुरू होता है और */ पर खत्म होता है, और कई lines तक span कर सकता है। यह code के एक हिस्से को temporarily disable करने या लंबे explanations लिखने के लिए उपयोगी है।
उदाहरण: Block Comments
/*
This program demonstrates block comments.
Everything in here is ignored by the compiler.
*/
fn main() {
println!("Block comments can span multiple lines");
}
Login to try C/C++/Java/PHP code in the editor
Documentation Comments
किसी function या item के ठीक ऊपर रखे Triple-slash /// comments documentation comments हैं। cargo doc जैसे tools इन्हें पढ़ते हैं ताकि आपके code के लिए अपने आप HTML documentation generate कर सकें।
उदाहरण: Documentation Comments
/// Prints a friendly greeting to standard output.
fn greet() {
println!("Hello from a documented function!");
}
fn main() {
greet();
}
Login to try C/C++/Java/PHP code in the editor
Comments Do Not Affect Execution
आप comments में चाहे कितना भी text डालें, compiled program बिल्कुल एक जैसा behave करता है क्योंकि compiler compilation शुरू होने से पहले ही comments strip कर देता है। यह comments को clarity के लिए पूरी तरह free बनाता है।
उदाहरण: Comments Do Not Affect Execution
fn main() {
let x = 5; // this comment does nothing to the value
println!("x is {}", x);
}
Login to try C/C++/Java/PHP code in the editor
- यह मानना कि comments execute होते हैं या program के behavior को किसी भी तरह प्रभावित करते हैं -- compiler इन्हें पूरी तरह skip कर देता है।
- comments का उपयोग यह explain करने के लिए करना कि obvious code *क्या* करता है बजाय इसके कि कोई non-obvious decision *क्यों* लिया गया।
- यह भूल जाना कि
//सिर्फ 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: