【Rust】Rust ~ コメント ~

◾️はじめに

https://dk521123.hatenablog.com/entry/2025/12/12/011304

で、「cargo doc」を学んだ。
折角なので、Rustのコメントについて学んでみる

目次

【1】通常のコメント
 1)//
 2)/* */
【2】ドキュメンテーションコメント
 1)サンプル
 2)ドキュメント作成

【1】通常のコメント

* ここは、C言語やJavaと同じ

https://doc.rust-jp.rs/rust-by-example-ja/hello/comment.html

1)//

* 行末までコメントアウト

2)/ /

* ブロックによって囲まれた部分をコメントアウト

【2】ドキュメンテーションコメント

*  ライブラリのドキュメンテーションとしてhtmlにパースされる

/// : このコメントの下の内容に関するドキュメント
//! : このコメントを含むソースのドキュメント

https://doc.rust-jp.rs/rust-by-example-ja/testing/doc_testing.html

1)サンプル

/// Adds two unsigned 64-bit integers and returns the result.
/// # Examples
/// ```
/// let sum = hello_lib::add(2, 3);
/// assert_eq!(sum, 5);
/// ```
///
/// # Panics
/// This function does not panic.
///
/// # Errors
/// This function does not return errors.
///
/// # Safety
/// This function is safe to call.
///
/// # Complexity
/// The time complexity is O(1).
/// The space complexity is O(1).
///
/// # Returns
/// The sum of the two input integers.
/// 
/// # Arguments
/// * `left` - The first integer to add.
/// * `right` - The second integer to add.
/// 
/// # Examples
/// ```
/// let result = hello_lib::add(10, 20);
/// assert_eq!(result, 30);
/// ```
/// # See Also
/// * `std::ops::Add` - The trait for addition operations.
/// # Notes
/// This function only works with unsigned 64-bit integers.
/// # References
/// * [Rust Documentation on Arithmetic](https://doc.rust-lang.org/std/ops/trait.Add.html)
/// # Category
/// Math Operations
/// # Since
/// 1.0.0
/// # Version
/// 1.0.0
pub fn add(left: u64, right: u64) -> u64 {
    left + right
}

/// Unit tests for the library functions.
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn it_works() {
        let result = add(2, 2);
        assert_eq!(result, 4);
    }
}

2)ドキュメント作成

# ドキュメント作成
cargo doc

# ドキュメント表示
cargo doc --opne

参考文献

https://zenn.dev/dokusy/articles/3b7292b1fe1937

関連記事

Rust ~ 環境構築編 ~
https://dk521123.hatenablog.com/entry/2023/04/22/234808
Rust ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2025/11/26/224648
Rust ~ cargoコマンド ~
https://dk521123.hatenablog.com/entry/2025/12/12/011304