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::*;
相关推荐
DongLi014 小时前
rustlings 学习笔记 -- exercises/05_vecs
rust
番茄灭世神21 小时前
Rust学习笔记第2篇
rust·编程语言
shimly1234561 天前
(done) 速通 rustlings(20) 错误处理1 --- 不涉及Traits
rust
shimly1234561 天前
(done) 速通 rustlings(19) Option
rust
@atweiwei1 天前
rust所有权机制详解
开发语言·数据结构·后端·rust·内存·所有权
shimly1234561 天前
(done) 速通 rustlings(24) 错误处理2 --- 涉及Traits
rust
shimly1234561 天前
(done) 速通 rustlings(23) 特性 Traits
rust
shimly1234561 天前
(done) 速通 rustlings(17) 哈希表
rust
shimly1234561 天前
(done) 速通 rustlings(15) 字符串
rust
shimly1234561 天前
(done) 速通 rustlings(22) 泛型
rust