Trait for a maximum value of a type

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.

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

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.

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

: )

2 Likes

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

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

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

Searching in the "std" docs apparently doesn't bring up results for other crates...

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.