rust 语法和语义 05 注释

本文详细介绍了Rust语言中的注释用法,包括行注释、整段注释及文档注释的不同形式,并展示了如何使用文档注释进行代码文档的自动生成。

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 文档,也可以将代码示例作为测试运行!


参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值