Rust从入门到实战系列三百一十三:代码跳转

本文介绍了Rust语言中如何在文档注释中实现代码跳转,包括标准库和模块间的跳转,以及常用的文档属性如`doc`,`inline`,`no_inline`,和`hidden`的用法,展示了rustdoc在生成文档时的灵活性。
摘要由CSDN通过智能技术生成

Rust 为我们提供一个非常强大的特性:可以在文档注释中实现代码跳转。
将以下代码添加到 src/lib.rs :
// in lib.rs
/// Add one to the given value and return a [Option] type
pub fn add_three(x: i32) -> Option {
Some(x + 3)
}
除了跳转到标准库中,我们还能跳转到项目中的其它模块。
// in lib.rs
mod a {
/// Add four to the given value and return a [Option] type
/// [crate::MySpecialFormatter]
pub fn add_four(x: i32) -> Option {
Some(x + 4)
}
}
struct MySpecialFormatter;
文档属性
下面是很常用的 #[doc] 属性,该属性可以被 rustdoc 所使用。
inline
可以用于内联文档, 而不是链接到一个单独的页面。
#[doc(inline)]
pub use bar::Bar;
/// bar docs
mod bar {
/// the docs for Bar
pub struct Bar;
}
no_inline
用于防止链接到单独的页面或其它地方。
// Example from libcore/prelude
#[doc(no_inline)]
pub use crate::mem::drop;
hidden
通过这个属性让 rustdoc 不要将下面的项包含在文档中:
// Example from the futures-rs library
#[doc(hidden)]
pub use self::async_await::;
对文档来说, rustdoc 被社区广泛采用,大家所看到的标准库文档也是基于此生成的。
完整的代码
doc-comments 的完整代码可以在这里找到
格式化输出
位置参数
1.🌟🌟
/
填 空 /
fn main() {
println!(“{0}, this is {1}. {1}, this is {0}”, “Alice”, “Bob”);// => Alice, this is B
assert_eq!(format!(“{1}{0}”, 1, 2), );
assert_eq!(format!(
, 1, 2), “2112”);
println!(“Success!”);
}
具名参数
2.🌟🌟
fn main() {
println!(“{argument}”, argument = “test”); // => “test”
/
填 空 /
assert_eq!(format!(“{name}{}”, 1, ), “21”);
assert_eq!(format!(
,a = “a”, b = ‘b’, c = 3 ), “a 3 b”);
/
修 复 错 误 */
// 具 名 参 数 必 须 放 在 其 它 参 数 后 面
println!(“{abc} {1}”, abc = “def”, 2);
println!(“Success!”)
}

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值