Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiently mutate a vector while also iterating over the same vector

Tags:

rust

I have a vector of structs, and I'm comparing every element in the vector against every other element, and in certain cases mutating the current element.

My issue is that you can't have both a mutable and immutable borrow happening at the same time, but I'm not sure how to reframe my problem to get around this without cloning either the current element or the entire vector, which seems like a waste since I'm only ever mutating the current element, and it doesn't need to be compared to itself (I skip that case).

I'm sure there's an idiomatic way to do this in Rust.

struct MyStruct {
    a: i32,
}

fn main() {
    let mut v = vec![MyStruct { a: 1 }, MyStruct { a: 2 }, MyStruct { a: 3 }];

    for elem in v.iter_mut() {
        for other_elem in v.iter() {
            if other_elem.a > elem.a {
                elem.a += 1;
            }
        }
    }
}
like image 957
anderspitman Avatar asked Mar 07 '18 03:03

anderspitman


1 Answers

The simplest way is to just use indices, which don't involve any long-lived borrows:

for i in 0..v.len() {
    for j in 0..v.len() {
        if i == j { continue; }
        if v[j].a > v[i].a {
            v[i].a += 1;
        }
    }
}

If you really, really want to use iterators, you can do it by dividing up the Vec into disjoint slices:

fn process(elem: &mut MyStruct, other: &MyStruct) {
    if other.a > elem.a {
        elem.a += 1;
    }
}

for i in 0..v.len() {
    let (left, mid_right) = v.split_at_mut(i);
    let (mid, right) = mid_right.split_at_mut(1);
    let elem = &mut mid[0];

    for other in left {
        process(elem, other);
    }
    for other in right {
        process(elem, other);
    }
}
like image 85
DK. Avatar answered Nov 04 '22 18:11

DK.