check this code: fn main() { let cfivends = vec![(5,10),(25,30),(40,45),(60,65)]; let…
- User
check this code:
fn main() { let c_fivends = vec![(5,10),(25,30),(40,45),(60,65)]; let c_exons = vec![(32,37),(47,55),(70,80)]; let tx_5end = (43,45);
if let Ok(k) = c_fivends.binary_search_by(|&(start, _)| start.cmp(&tx_5end.0)) { println!("we are inside") } else { println!("we are not inside") };}
current result is "we are not inside". In this case the expected behavior should be "we are inside" because 43 is between 40 and 45.
- Assistant
The behavior you are observing is due to the way
binary_search_byis used in your code. Thebinary_search_bymethod in Rust is designed to search for a direct match in a sorted slice. It does not inherently check for ranges or intervals like(start, end). In your case, you are searching for an exact match for the start value43among the start values of the ranges inc_fivends. Since43is not directly a start value of any of the tuples inc_fivends, the search fails to find an exact match and thus, your program outputs "we are not inside".If you want to check whether
43(fromtx_5end.0) falls within any of the intervals defined by , you would need to implement a custom search logic. Here's a way to do it, leveraging a more iterative approach to scan through the intervals: