how to

functional-programming

Jan 15, 2024
langsrust
1 Minutes
55 Words

Find in a Vec the smallest f(x).unwrap() where f(x) is not None

1
fn func(x: f64) -> Option<f64> {
2
if x > 2.5 {
3
Some(x + 100.0)
4
} else {
5
None
6
}
7
}
8
fn main() {
9
let vec = vec![6.0, 5.0, 4.0, 3.0, 2.0, 1.0];
10
let min_odd = vec
11
.iter()
12
.filter_map(|x| func(*x))
13
.min_by(|x, y| x.partial_cmp(&y).unwrap_or(std::cmp::Ordering::Equal));
14
15
println!("{min_odd:?}");
1 collapsed line
16
}
Article title:functional-programming
Article author:Julyfun
Release time:Jan 15, 2024
Copyright 2025
Sitemap