Expand description
num-lazy helps you write numbers for generic-typed functions.
use num_lazy::declare_nums;
use num_traits::Float;
// Assign to generic type T.
// Important. Use the braces!
declare_nums!{T}
fn circumference<T: Float>(radius: T) -> T {
two!() * 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 in the module or crate root.