Crates.io | min_max_traits |
lib.rs | min_max_traits |
version | 0.1.0 |
source | src |
created_at | 2022-01-16 08:01:48.412181 |
updated_at | 2022-01-16 08:01:48.412181 |
description | Traits for MIN and MAX associated constants |
homepage | |
repository | https://github.com/JohnScience/min_max_traits |
max_upload_size | |
id | 514675 |
size | 18,008 |
At the time of writing, all primitive numeric types in Rust provide MIN
and MAX
associated constants, which nonetheless do not belong to any trait.
One commonly used crate, num-traits
, offers many useful traits for numeric types. However, the closest
analogue of min_max_traits::Min
and min_max_traits::Max
offered by num-traits
at the time of writing
is num_traits::Bounded
, which requires implementation of min_value()
and max_value()
functions. Since const_fn_trait_bound
feature is in the works, num_traits::Bounded
cannot be used in generic implementations of constant functions
relying on MIN
and MAX
associated constants, at least on stable Rust.
These traits can be useful, for example, to generically implement associated constants storing the greatest length of primitive integers when converted to strings.
Excerpt from Rust's reference:
The unsigned integer types consist of:
Type | Minimum | Maximum |
---|
u8
| 0 | 28-1
u16
| 0 | 216-1
u32
| 0 | 232-1
u64
| 0 | 264-1
u128
| 0 | 2128-1
The signed two's complement integer types consist of:
Type | Minimum | Maximum |
---|
i8
| -(27) | 27-1
i16
| -(215) | 215-1
i32
| -(231) | 231-1
i64
| -(263) | 263-1
i128
| -(2127) | 2127-1
The IEEE 754-2008 "binary32" and "binary64" floating-point types are f32
and
f64
, respectively.