-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ER] Array.max and min ? #78504
Comments
This will work: |
Oh you said that already, sorry. |
This works on nightly: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=c2434427de054bbbb32950461920bb70. Not sure how to enforce the length is greater than 0. |
I think this would compile efficiently(and is the same as jyn's with an added assert which might change compilation behavior):
|
Now there are Nightly ways to enforce N > 0. A related function could be array::fold. |
I've landed here multiple times looking for a method on let arr: [i32; 4] = [10, 20, 30, 40];
let max = arr.reduce(i32::max); |
When you want to compute the max or min of three or more items you have various options:
You can also introduce a macro:
But an array method may solve the problem better, without macros (code untested) and without unwraps:
The text was updated successfully, but these errors were encountered: