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::*;
相关推荐
SomeOtherTime10 小时前
第 11 讲(大结局):编译期元编程与零拷贝架构——卫生声明宏(macro_rules!)、过程宏 AST 手术刀与 Serde 零反射序列化的终极合龙
rust
SomeOtherTime14 小时前
番外篇(第 12 讲):走向工业界深水区——从物理定律看 Rust 在真实工程中的“降维打击”与“阿喀琉斯之踵”
开发语言·rust
Kapaseker15 小时前
Rust 桌面开发到底怎么选?先看这篇文章!
rust
奶油话梅糖16 小时前
容器启停 2.5 毫秒:11 天 1,700 Star 的 Kiri,用一个 900 KB 的 PID 1 干掉了 runc 和 dockerd
前端·人工智能·rust
wflynn1 天前
GitHub 今日推荐|kiri:clone3 系统调用直达容器,2.5 毫秒启动无 runc 中间层
docker·rust·kubernetes·开源·github·containers
SomeOtherTime1 天前
第 4 讲:突破静态枷锁——智能指针(Box/Rc/Arc)与内部可变性(Cell/RefCell/UnsafeCell)的内存暗道
rust
SomeOtherTime2 天前
第 9 讲:异步并发与 Pin 的底层解剖——async/await 无栈协程状态机脱糖、自引用死锁的终极救赎与 Future/Waker 驱动模型
rust
wflynn2 天前
GitHub 今日推荐|Stela:在 PC 上直接启动 macOS 的翻译层
rust·开源·github
aifannihao2 天前
Rust 1.100 的 DropGuard:用 Python 实测失败回滚与吞异常
python·rust·异常处理·标准库·contextlib
鶴哥只手遮天3 天前
《rustc 内幕》第 1 章 全景:rustc 编译流水线与 crate 版图
后端·rust·开源