5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Feature フラグをドキュメントする (doc_cfg)

Last updated at Posted at 2022-07-16

Feature フラグの存在を伝える

syn クレートのドキュメント より:

doc_cfg.png

buffer モジュールは parsing フラグが有効な場合のみ利用できると一目で分かります。

Cargo.toml

docs.rs で doc_cfg 属性を有効にします:

Cargo.toml
[package.metadata.docs.rs]
all-features = true
targets = ["x86_64-unknown-linux-gnu"]
rustdoc-args = ["--cfg", "doc_cfg"]

ソース

#[cfg(doc(..)] が feature を表示する機能です。 Unstable なフィーチャーなので有効化します:

src/lib.rs
#![cfg_attr(doc_cfg, feature(doc_cfg))]

cfg_attr ガードにより、 doc_cfg が未定義の場合は無視しています。

buffer モジュールの宣言は:

src/lib.rs
#[cfg(feature = "parsing")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
pub mod buffer;

これで docs.rs では冒頭のように表示されます:

doc_cfg.png

ローカルでも doc_cfg フィーチャーを使う

コメントで教えていただきました。ありがとうございます!

$ # ドキュメント生成
$ cargo +nightly rustdoc -- --cfg doc_cfg

$ # ビルド後にブラウザで開く
$ cargo +nightly rustdoc --open -- --cfg doc_cfg

Nightly を使っているなら、以下のようなエイリアスも有効です:

.cargo/config
[alias]
d = "rustdoc -- --cfg doc_cfg"
do = "rustdoc --open -- --cfg doc_cfg"

ただしクレートによって #[doc(cfg(..))] に使っている属性名は異なります!
doc_cfg の他に docsrs も一般的な名称です。

5
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?