How vec.dedup_by works

have a code like this

struct Field {
    name: String,
    is_opt: bool,
    is_multi: bool,
}

fn dedup_fields(fields: &mut Vec<Field>) {
    fields.dedup_by(|(a, b)|
        if a.name == b.name {
            a.is_opt |= b.is_opt;
            a.is_multi = true;
            true
        } else {
             false
        }
    });
}

in output if two fields are same it dedups but dont make is_multi true and makes is_opt true

From the docs:

/// The `same_bucket` function is passed references to two elements from the vector and
/// must determine if the elements compare equal. The elements are passed in opposite order
/// from their order in the slice, so if `same_bucket(a, b)` returns `true`, `a` is removed.

I.e. a is the element being removed.

1 Like