Bug: 171359139

Clone this repo:
  1. f8964be Upgrade rust/crates/const_fn to 0.4.6 by Joel Galenson · 3 years, 4 months ago main master
  2. 02c3714 [LSC] Add LOCAL_LICENSE_KINDS to external/rust/crates/const_fn by Bob Badour · 3 years, 4 months ago
  3. ae47280 Upgrade rust/crates/const_fn to 0.4.3 by Haibo Huang · 3 years, 9 months ago
  4. b3b422d Add Android.bp with --copy-out flag. by Chih-Hung Hsieh · 3 years, 9 months ago
  5. fcfc9e6 Import const_fn-0.4.2 by Chih-Hung Hsieh · 3 years, 9 months ago


crates.io docs.rs license rustc build status

An attribute for easy generation of const functions with conditional compilations.


Add this to your Cargo.toml:

const_fn = "0.4"

Compiler support: requires rustc 1.31+


use const_fn::const_fn;

// function is `const` on specified version and later compiler (including beta and nightly)
pub const fn version() {
    /* ... */

// function is `const` on nightly compiler (including dev build)
pub const fn nightly() {
    /* ... */

// function is `const` if `cfg(...)` is true
pub const fn cfg() {
    /* ... */

// function is `const` if `cfg(feature = "...")` is true
#[const_fn(feature = "...")]
pub const fn feature() {
    /* ... */


This crate is proc-macro, but is very lightweight, and has no dependencies.

You can manually define declarative macros with similar functionality (see if_rust_version), or you can define the same function twice with different cfg. (Note: the former approach requires more macros to be defined depending on the number of version requirements, the latter approach requires more functions to be maintained manually)


Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.