check this code: fn findappropriaterange(ranges: &Vec<(u32, u32, u32)>, x: u32) ->…
- User
check this code:
fn find_appropriate_range(ranges: &Vec<(u32, u32, u32)>, x: u32) -> Option<(u32, u32, u32)> { let insertion_point = ranges.binary_search_by(|&(start, end, _)| { if x < start { std::cmp::Ordering::Greater } else if x >= end { std::cmp::Ordering::Less } else { std::cmp::Ordering::Equal } });
match insertion_point { Ok(index) => Some(ranges[index]), Err(0) => None, // x is before the first range Err(index) => Some(ranges[index - 1]), }}
fn main() { let ranges = vec![(0, 10, 0), (15, 20, 1), (25, 30, 2), (40, 50, 3)]; let target_start = (23, 25); // we’re only interested in the start of the target tuple for comparison
match find_appropriate_range(&ranges, target_start.0) { Some(range) => println!("Found range that satisfies condition: {:?}", range), None => println!("No suitable range found."), }}
the result is Found range that satisfies condition: (15, 20, 1)
but that is not true, because 23 is > than 20
- Assistant