rust 語法和語義 05 註釋

rust 語法和語義 05 註釋

註釋:comments

rust的註釋主要分爲兩類:行註釋 line comments文檔註釋 doc comments

行註釋 line comments

c 一樣,使用 // 開頭。

// Line comments are anything after ‘//’ and extend to the end of the line.

let x = 5; // This is also a line comment

// If you have a long explanation for something, 
// you can put line comments next to each other.
// Put a space between the // and your comment so that it’s
// more readable.

整段註釋

c 一樣,使用 /* */ 表示。

/*
this is block comments,
you can put line comments next to each other.
 */

文檔註釋 doc comments

使用 /// 表示文檔註釋 。並內建 Markdown 標記支持。

/// Adds one to the number given.
///
/// # Examples
///
/// ```
/// let five = 5;
///
/// assert_eq!(6, add_one(5));
/// # fn add_one(x: i32) -> i32 {
/// #     x + 1
/// # }
/// ```
fn add_one(x: i32) -> i32 {
    x + 1
}

包含項註釋

使用 //! 註釋那些包含這個註釋的 crate,mod,或者function。而不是位於註釋以後的內容。

經常用於 crate的根文件 lib.rs 或者 模塊的根文件 mod.rs

//! # The Rust Standard Library
//!
//! The Rust Standard Library provides the essential runtime
//! functionality for building portable Rust software

文檔註釋生成

可以使用 rustdoc 工具來將文檔註釋生成爲 HTML 文檔,也可以將代碼示例作爲測試運行!


參考

發佈了59 篇原創文章 · 獲贊 20 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章