How to use `reduce` in Iterator trait

Hi! I am reading Rust std now and I found something confusing. In this example for Iterator::max below

    [2.4, f32::NAN, 1.3]

How about this reduce here? The std tells me it should take a closure as a parameter but there is only a return value f32::max here? The result, 2.4 is also confusing. Could you explain what happen in reduce here?

reduce doesn't just take a closure. It takes FnMut(Self::Item, Self::Item) -> Self::Item, which means any closure or function that takes 2 arguments and returns an item.

f32::max is a method on the f32 type. It takes two arguments: one is self, and the other is the other number. You typically call it like x.max(y), but f32::max(x,y) works too. Rust calls that universal function call syntax (UFCS).


Thank you so much for this detailed answer! I get it now! :yum:

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.