[][src]Crate static_assertions


Compile-time assertions to ensure that invariants are met.

All assertions within this crate are performed at compile-time. This allows for finding errors quickly and early when it comes to ensuring certain features or aspects of a codebase. These macros are especially important when exposing a public API that requires types to be the same size or implement certain traits.


This crate is available on crates.io and can be used by adding the following to your project's Cargo.toml:

static_assertions = "0.3.4"

and this to your crate root (main.rs or lib.rs):

extern crate static_assertions;


Very thorough examples are provided in the docs for each individual macro. Failure case examples are also documented.


Due to implementation details, some macros can only be used normally from within the context of a function. To use these macros in other contexts, a unique label must be provided.

This example deliberately fails to compile
// error: expected item after attributes
const_assert!(true == true);

This can be fixed via:

const_assert!(label; true == true);

This can be followed at issue #1.

Labeling Limitation Fix

The labeling workaround is not necessary (and is removed) when compiling on nightly Rust with the nightly feature flag enabled. This can be done by having the following in your project's Cargo.toml:

version  = "0.3.4"
features = ["nightly"]

To compile with nightly Rust, run the following in your shell or command prompt of choice:

rustup install nightly
cargo +nightly build

Notice that this also requires enabling the underscore_const_names nightly Rust feature:

This example is not tested

const_assert!(true != false);

fn main() {
    const_assert!(false != true);


See CHANGELOG.md for a complete list of what has changed from one version to another.


This project is made freely available (as in free beer), but unfortunately not all beer is free! So, if you would like to buy me a beer (or coffee or more), then consider supporting my work that's benefited your project and thousands of others.

Become a Patron! Buy me a coffee



Asserts that a given configuration is set.


Asserts that types are equal in size.


Asserts that values pointed to are equal in size.


Asserts that values are equal in size.


Asserts that types are equal.


Asserts that the type has the given fields.


Asserts that the type implements all of the given traits.


Asserts that the type implements all of the given traits.


Asserts that types are not equal.


Asserts that the type does not implement all of the given traits.


Asserts that the type does not implement any of the given traits.


Asserts that the traits are object-safe.


Asserts that constant expressions evaluate to true.


Asserts that constants are equal in value.


Asserts that constants are not equal in value.