Expand description
num-lazy helps you write numbers for generic-typed functions.
It is recommended to use num-lazy along with numeric-literals.
Use num-lazy to access macros for constants and special values, while using numeric_literals for parsing
floats or numeric literals.
use num_lazy::declare_nums;
use numeric_literals::replace_numeric_literals;
use num_traits::Float;
declare_nums!{@constant T}
declare_nums!{@special T}
// declare_nums!{@literal T} // Recommend using `numeric_literals` instead
// Or use this to declare all macros:
// declare_nums!{T}
#[replace_numeric_literals(T::from(literal).unwrap())]
fn circumference<T: Float>(radius: T) -> T {
2 * pi!() * radius
}See what numbers are declared in declare_nums.
If you declare_nums!() in the root of your crate, you don’t even need
to import the macros to submodules. This will not re-export the macros
to the public crate.
lib.rs or main.rs
ⓘ
use num_lazy::declare_nums;
declare_nums!{T}
// My submodules
pub mod generic_math;generic_math.rs
ⓘ
pub fn circle_area<T: Float>(radius: T) -> T {
pi!() * radius * radius
}If you want to declare the numbers in the root of your crate but keep the macro contained in a module, you can simply:
lib.rs or main.rs
ⓘ
mod generics {
use num_lazy::declare_nums;
declare_nums!{T}
}generic_math.rs
ⓘ
use crate::generics::*;
pub fn circle_area<T: Float>(radius: T) -> T {
pi!() * radius * radius
}Macros§
- declare_
nums - Declare commonly used num generics.