Compile-time assertions for Rust, brought to you by Nikolai Vazquez.
 = "1.1.0"
and this to your crate root (
extern crate static_assertions;
This crate exposes the following macros:
Q: When would I want to use this?
A: This library is useful for when wanting to ensure properties of constants, types, and traits.
With the release of 1.39,
str::lencan be called in a
const_assert!, one can check that a string generated from elsewhere is of a given size:
const DATA: &str = include_str!; const_assert!;
Have a type that absolutely must implement certain traits? With
assert_impl_all!, one can ensure this:
Q: How can I contribute?
A: A couple of ways! You can:
Attempt coming up with some form of static analysis that you'd like to see implemented. Create a new issue and describe how you'd imagine your assertion to work, with example code to demonstrate.
Implement your own static assertion and create a pull request.
Give feedback. What are some pain points? Where is it unpleasant?
Write docs. If you're familiar with how this library works, sharing your knowledge with the rest its users would be great!
Q: Will this affect my compiled binary?
A: Nope! There is zero runtime cost to using this because all checks are at compile-time, and so no code is emitted to run.
Q: Will this affect my compile times?
A: Likely not by anything perceivable. If this is a concern, this library can be put in
 = "1.1.0"
and then assertions can be conditionally run behind
However, the assertions will only be checked when running
cargo test. This somewhat defeats the purpose of catching false static conditions up-front with a compilation failure.
Q: What is
A: It's a way of creating an unnamed constant. This is used so that macros can be called from a global scope without requiring a scope-unique label. This library makes use of the side effects of evaluating the
constexpression. See the feature's tracking issue and issue #1 for more info.
for a complete list of what has changed from one version to another.
This project is released under either:
at your choosing.