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 文檔,也可以將代碼示例作爲測試運行!