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::*;
相关推荐
wflynn5 小时前
GitHub 今日推荐|Stela:在 PC 上直接启动 macOS 的翻译层
rust·开源·github
aifannihao6 小时前
Rust 1.100 的 DropGuard:用 Python 实测失败回滚与吞异常
python·rust·异常处理·标准库·contextlib
鶴哥只手遮天13 小时前
《rustc 内幕》第 1 章 全景:rustc 编译流水线与 crate 版图
后端·rust·开源
SomeOtherTime1 天前
Rust 闭包语法和原理(AI回答)
开发语言·后端·rust
SomeOtherTime2 天前
Rust集合(AI回答)
开发语言·后端·rust
SomeOtherTime2 天前
Rust 泛型与特性讲解(AI回答)
rust
青川Lapis2 天前
RUST:错误处理 thiserror + anyhow 工程化
rust
SomeOtherTime3 天前
Rust生命周期介绍(AI回答)
开发语言·后端·rust
SomeOtherTime3 天前
Rust所有权(AI回答)
开发语言·后端·rust
SomeOtherTime3 天前
Rust 面向对象介绍(AI回答)
开发语言·python·rust