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.

0 Likes

#2

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

0 Likes

#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.

0 Likes

#4

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

: )

2 Likes

#5

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

0 Likes

#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

0 Likes

#7

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

0 Likes

#8

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

0 Likes

closed #9

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

0 Likes