[Mesa-dev] Rust drivers in Mesa
contact at taylor.fish
contact at taylor.fish
Sat Oct 3 02:42:47 UTC 2020
On 2020-10-02 at 17:53 Jason Ekstrand <jason at jlekstrand.net> wrote:
> b. Pulling two items out of a list and looking at them is painful.
> Unfortunately, this is a surprisingly common operation in compiler
> passes. The usual way to get around it is to structure your code such
> that you first look up all the information you want with non-mut
> things and then you take a mut reference to the thing you want to
> change and change it.
You can obtain concurrent &mut references to multiple elements of a data
structure using iterators:
let mut v = vec![0, 1, 2, 3, 4, 5];
let mut iter = v.iter_mut();
let x = iter.nth(3).unwrap(); // mutable ref to element 3
let y = iter.next().unwrap(); // mutable ref to element 4
*x += 100;
*y += 200;
println!("{:?}", v); // => [0, 1, 2, 103, 204, 5]
I can't say this is the best solution in all cases, but it may be useful
some of the time.
More information about the mesa-dev
mailing list