如何在`cargo doc`生成的文档中获取功能需求标签? [英] How to get a feature requirement tag in the documentation generated by `cargo doc`?
问题描述
如果您查看
我也想为我的 crate 启用此功能,如何实现?
坏消息是:它现在是每晚才有的功能.
好消息是:docs.rs 默认每晚使用.
要使其正常工作,您只需要启用 doc_cfg
功能 并将#doc(cfg)
应用到正在记录的项目
#![feature(doc_cfg)]#[doc(cfg(feature = 宏"))]pub fn 测试(){}
由于这是一项仅限夜间使用的功能,您可能不想一直启用它.tokio
在其 Cargo.toml
中定义了以下内容以仅在 docs.rs 上启用此功能:
# docs.rs 特定配置[package.metadata.docs.rs]# 记录所有特征全功能 = 真# 定义配置属性`docsrs`rustdoc-args = [--cfg",docsrs"]
然后他们使用
//仅在以下情况下启用 `doc_cfg` 功能//定义了 `docsrs` 配置属性#[cfg_attr(docsrs, feature(doc_cfg))]#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]pub fn 测试(){}
If you look at the Tokio docs on docs.rs there's a blue tag indicating that a feature must be activated in order to access this API:
I would like to enable this for my crate as well, how can this be done?
The bad news is: It's a nightly-only feature for now.
The good news is: docs.rs uses nightly by default.
To get this to work all you need is to enable the doc_cfg
feature and apply #doc(cfg)
to the item being documented
#![feature(doc_cfg)]
#[doc(cfg(feature = "macros"))]
pub fn test() {}
Because this is a nightly-only feature, you probably don't want to enable it all the time. tokio
defines the following in its Cargo.toml
to only enable this feature on docs.rs:
# docs.rs-specific configuration
[package.metadata.docs.rs]
# document all features
all-features = true
# defines the configuration attribute `docsrs`
rustdoc-args = ["--cfg", "docsrs"]
and then they use
// only enables the `doc_cfg` feature when
// the `docsrs` configuration attribute is defined
#[cfg_attr(docsrs, feature(doc_cfg))]
#[cfg_attr(docsrs, doc(cfg(feature = "macros")))]
pub fn test() {}
这篇关于如何在`cargo doc`生成的文档中获取功能需求标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!