I would like a generic integer function to compare integers to 0
and then return an option depending upon if the integer is greater than zero. Here is my current attempt which doesn't compile:
fn greater_than_zero_or_non<T>(x: T) -> Option<T>
where
T: std::cmp::PartialOrd,
{
if x > 0 {
Some(x)
} else {
None
}
}
The compiler is complaining about the integer within the comparison:
error[E0308]: mismatched types
--> src/ledger/address.rs:15:12
|
15 | if x > 0 {
| ^ expected type parameter, found integer
|
= note: expected type `T`
found type `{integer}`
= help: type parameters must be constrained to match other types
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
Any help in understanding why this doesn't compile and how one could write something similar without having to use multiple functions for multiple sizes of integers would be amazing.
As I write this I suppose that I could use u64
since all of my input integers are u16
and u32
, but I still would like to understand if there's a proper way to accomplish this.