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

Documentation Comments लिखना

Documentation comments वे friendly explanations हैं जो आप अपने code के ऊपर लिखते हैं जो एक असली, browsable manual में बदल जाते हैं।
Syntax
rust
/// Documents the next item
//! Documents the enclosing item

/// # Examples
/// # Panics

Documenting a Function with ///

किसी function के ऊपर सीधे /// comments रखना describe करता है कि यह क्या करता है, और cargo doc उस function की documentation page generate करने के लिए यह text उपयोग करता है।

उदाहरण: Documenting a Function with ///

markup
/// Calculates the area of a rectangle given its width and height.
fn area(width: f64, height: f64) -> f64 {
    width * height
}

fn main() {
    println!("Area: {}", area(3.0, 4.0));
}

Documenting a Module with //!

//! comments, आमतौर पर किसी file के बिल्कुल top पर रखे गए, enclosing item को खुद document करते हैं (जैसे पूरा module) बजाय उनके बाद आने वाले किसी भी code के।

उदाहरण: Documenting a Module with //!

markup
//! This module provides simple geometry helper functions.

fn perimeter(width: f64, height: f64) -> f64 {
    2.0 * (width + height)
}

fn main() {
    println!("Perimeter: {}", perimeter(3.0, 4.0));
}

Including a Usage Example

अच्छी documentation comments में अक्सर एक fenced code example शामिल होता है जो दिखाता है कि function कैसे call करें, जो readers के लिए helpful भी है और अपने आप चलने वाले doc test के रूप में भी काम करता है।

उदाहरण: Including a Usage Example

markup
/// Converts Celsius to Fahrenheit.
///
/// ```
/// let f = my_project::to_fahrenheit(0.0);
/// assert_eq!(f, 32.0);
/// ```
pub fn to_fahrenheit(celsius: f64) -> f64 {
    celsius * 9.0 / 5.0 + 32.0
}

fn main() {
    println!("{}", to_fahrenheit(0.0));
}

Generating HTML Docs

cargo doc --open किसी project की सभी doc comments से एक पूरी HTML documentation site build करता है और इसे सीधे आपके web browser में खोलता है।

उदाहरण: Generating HTML Docs

bash
cargo doc --open

⚠️ Run this in your own terminal or Node.js environment.

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. Regular // comments उपयोग करना जब /// चाहिए ताकि comment असल में cargo doc से documentation के रूप में उठाया जाए।
  2. यह भूल जाना कि //! enclosing item को document करता है (जैसे पूरा module) बजाय इसके बाद आने वाले अगले item के।
  3. Public functions के लिए doc comments में एक runnable example include न करना, एक free doc test का आसान मौका गंवाते हुए।
चैप्टर सारांश
  • /// इसके तुरंत बाद आने वाले item को document करता है, जैसे एक function या struct।
  • //! enclosing item को खुद document करता है, आमतौर पर किसी module या crate के top पर उपयोग होता है।
  • cargo doc --open इन comments से HTML documentation generate करता है और इसे एक browser में खोलता है।
  • अच्छी तरह लिखी doc comments में अक्सर एक runnable example शामिल होता है, जो एक doc test के रूप में भी काम करता है।
🔒

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.