Clippy warning when if then else should be the same

⚓ Rust    📅 2026-01-20    👤 surdeus    👁️ 15      

surdeus

Warning

This post was published 82 days ago. The information described in this article may have changed.

Here is an example where the if then else cases do the same thing but cannot be collapsed into one case:

fn max_index<T : PartialOrd>( v : &[T] ) -> usize {   
    let mut imax = v.len();
    for (i, v_i) in v.iter().enumerate() {
        if imax == v.len() {
            imax = i;   
        } else if v[imax] < *v_i {
            imax = i;
        }
    }
    imax
}
fn main() {
    let v    = vec![0, 1, 2, 3];
    let imax = max_index(&v); 
    println!( "max = {}", v[imax] );
}

2 posts - 2 participants

Read full topic

🏷️ Rust_feed