Trait for a maximum value of a type


#1

There are constants like std::u32::MAX, but AFAIK there’s no generic way to get the maximum.

I was writing an algorithm that works on Ord, but I needed a special value that is higher than all other values. I’ve had to roll my own trait for this, but It’d be nice to have that in stdlib.


#2

Why did you need that value? Were you calculating some minimum and needed a default?


#3

Yes, exactly. I’m creating and searching a binary tree, and I need special values for the leaves and a starting point when searching it.

I know I could theoretically avoid having a placeholder maximum, but having it makes the code simpler.


#4

http://doc.rust-lang.org/num/num/traits/trait.Bounded.html

: )


#5

I’d also like to refer you to the min() (also max()) method that is implemented for Iterator<T> where T: Ord.


#6

That’s what I was looking for! It didn’t come up in the search though :frowning:

http://doc.rust-lang.org/std/?search=maximum

http://doc.rust-lang.org/std/?search=bounded


#7

No problem. I agree that traits sometimes makr for suboptimal discoverability. Searching for min immediately brought me there, luckily.


#8

Searching in the “std” docs apparently doesn’t bring up results for other crates…