How to prepare a crate for cargo publish when it needs to be built with the nightly rust toolchain?

I have extracted some functionality into a crate that I think would also be useful to others, so i would like to publish it on crates.io. However, my code makes use of some features that are not in stable rust. How do I correctly package this crate?

I would like to explicitly state what files to include, as explained in https://doc.rust-lang.org/cargo/reference/publishing.html :

[package]
# ...
include = [
    "**/*.rs",
    "Cargo.toml",
]

Should I add the rust-toolchain.toml file to the includes? It looks like this:

[toolchain]
channel = "nightly"

Or is there a better way, e.g. to specify exactly which features from nightly the crate needs? After all, it’s possible that some future nightly versions will not support those features anymore.

Or maybe the #![feature(...)]) annotations in src/lib.rs are already sufficient for this?

Or maybe the rust-version field in the Cargo.toml manifest should be used for this?

All of these uncertainties boil down to the question in the title: How to prepare a crate for cargo publish when it needs to be built with the nightly rust toolchain?

There is no special consideration for nightly crates nor is there a standardized way to indicate that it relies on unstable features as far as I know.

  • rust-toolchain.toml is not considered by downstream builds
  • there is rust-version that you can specify in your Cargo.toml, but specifying a nightly version either doesn’t work or is ignored

The best way to publish a nightly crate is to make it clear in your readme that it relies on the nightly compiler. And even then, a user attempting to use it on a stable release will see #![feature] may not be used on the stable release channel” errors – which is hopefully clear enough.

3

One way is to use Feature Gates.
(This only makes sense if there is some basic functionality that works in stable).

[features]
# Defines a feature named `nightly` that does not
# enable any other features.
nightly = []

Then we can annotate code with #[cfg(feature = "nightly")] like this:

#[cfg(feature = "nightly")]
impl std::iter::Step for TileIndex {
...
}

This code will only be included if the feature flag is set by the consumer. That is, they need to either use cargo build --features nightly or specify the dependency in their own Cargo.toml with the feature flag:

derive_more = { version = "1", features = ["nightly"] }

This makes it possible to ship the crate with some functionality that works in stable rust, and optionally make the rest of the functions available.

The #[cfg(feature = "nightly")] annotation can also be used before functions, or before code blocks within a function. However, I ran into one such case where the skipped code would cause syntax errors (because I used yield there, which is not yet stable). In that case, the crate cfg_if helps:

cfg_if::cfg_if! {if #[cfg(feature = "nightly")] {
impl IntoIterator for MovementRange {
...
}
}

In either case, this only skips including the code, but it would still not work with cargo +stable build, because the #![feature(...)] annotations may only be used in nightly. To skip them too, you can use cfg_attr which does the same thing as cfg, but for attributes:

// We have code that uses the nightly toolchain. It is gated behind the "nightly" crate feature flag.
// This means the crate can still be used with stable rust - it will just not have all functions.
#![cfg_attr(feature = "nightly", feature(step_trait))]
#![cfg_attr(feature = "nightly", feature(coroutines))]
#![cfg_attr(feature = "nightly", feature(iter_from_coroutine))]
#![cfg_attr(feature = "nightly", feature(impl_trait_in_assoc_type))]

To test where such an attribute was used, you can first set it to conditional compilation like I just showed, and then build with cargo +nightly build, which does not automatically contain the feature flag "nightly" and will tell you what code would need the feature, so you can go and exclude it with #[cfg( feature = "nightly")].

I don’t know whether this is a good way. But after these changes:

  • My crate builds basic functionality with cargo +stable build
  • All functions still work with cargo +nightly build
  • Note: Rustdoc has experimental support for annotating the documentation to indicate which features are required to use certain APIs.

    • However, this itself is an unstable feature.. You can again feature-gate that, but in the end that means that only the cargo +nightly doc will show this information.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật