rust注释

一、普通注释

复制代码
// 这是第一种注释方式

/* 这是第二种注释方式 */
/*
多行注释
多行注释
多行注释
 */

二、文档注释

复制代码
///外部行文档注释。为接下来的项生成帮助文档
//! 内部行文档注释。为注释所属于的项生成帮助文档

/**...*/外部块文档注释。为接下来的项生成帮助文档
/*!...*/内部块文档注释。为注释所属于的项生成帮助文档

实例

复制代码
/// Adds one to the number given.
///
/// # Examples
///
/// ```
/// let x = add(1, 2);
///
/// ```
fn add(a: i32, b: i32) -> i32 {
    return a + b;
}
fn main() {
    println!("{}",add(2,3));
}

用 cargo doc 构建文档到 target/doc。注释转换成HTML格式的说明文档。

用 cargo test --doc 仅运行文档测试。

这些命令最终会调用 rustdoc。

三、文档属性

下面是一些最常使用的 #[doc] 属性

1.inline

用于内联文档,而不是链接到单独的页面。

例子

复制代码
#[doc(inline)]
pub use bar::Bar;
/// bar的文档
mod bar {
     /// Bar的文档
     pub struct Bar;
} 

2.no_inline

用于防止链接到单独的页面或其他位置。

例子

复制代码
#[doc(no_inline)]
pub use crate::mem::drop; 

3.hidden

使用此属性来告诉 rustdoc 不要包含此项到文档中:

例子

复制代码
#[doc(hidden)]
pub use self::async_await::*;
相关推荐
shimly1234567 分钟前
(done) 速通 rustlings(17) 哈希表
rust
shimly12345644 分钟前
(done) 速通 rustlings(15) 字符串
rust
shimly1234562 小时前
(done) 速通 rustlings(22) 泛型
rust
yezipi耶不耶2 小时前
我在 RTMate 里使用的高并发连接管理利器: DashMap
websocket·rust
初恋叫萱萱8 小时前
深入解析 Rust + LLM 开发:手把手教你写一个 AI 运维助手
运维·人工智能·rust
shimly12345616 小时前
(done) 速通 rustlings(9) 分支跳转
rust
shimly12345621 小时前
(done) 速通 rustlings(4) 变量声明
rust
shimly1234561 天前
(done) 速通 rustlings(11) 向量vector及其操作
rust
shimly1234561 天前
(done) 速通 rustlings(3) intro1 println!()
rust
shimly1234561 天前
(done) 速通 rustlings(12) 所有权
rust