Linked Questions
47 questions linked to/from How to get mutable references to two array elements at the same time?
9 votes
3 answers
4k views
How can I use multiple items in a Vec at a time in Rust? [duplicate]
I've been fighting with the borrow checker for a little bit... the gist of what I want to do is this: #[derive(Debug)] struct SomeStruct { value: String, } impl SomeStruct { fn new(value: &...
11 votes
1 answer
4k views
How do I extract two mutable elements from a Vec in rust [duplicate]
I'm trying to extract two elements from a Vec, which will always contain at least two elements. These two elements need to be extracted mutably as I need to be able to change values on both as part of ...
3 votes
2 answers
3k views
How to implement multiple mutable borrows of a vector in Rust? [duplicate]
I am implementing matrices in Rust. The code is adapted for the example, but there might be minor mistakes: #[derive(Debug, PartialEq)] pub struct Matrix<T> { inner: Vec<Vec<T>>,...
2 votes
1 answer
2k views
Parallel write to array with an indices array [duplicate]
I'm having trouble understanding the concurrency model in Rust coming from C++. My array is to be accessed concurrently using another array that defines the indices. For example (Pseudocode): let ...
1 vote
2 answers
2k views
In Rust how to merge two sub vectors within a vector of vectors? [duplicate]
In Rust, I have a Vec of Vec. I want to merge the content of one sub Vec with another one. Here is my code: let mut a = vec![vec![1, 2, 3], vec![3, 4, 5, 6], vec![8, 9]]; a[0].append(&mut a[1]); ...
1 vote
1 answer
757 views
Structs containing mutable slices [duplicate]
I'm trying to understand lifetimes and storing mutable slices inside a struct. I came up with this example with a struct with a slice and a take function that will return n elements (if present) and ...
0 votes
2 answers
448 views
Is RefCell an appropriate workaround to borrow two mutable elements from a vector? [duplicate]
Consider this toy example of "fighting" two random "players": #[derive(Clone)] struct Player { name: String, health: i32, attack: i32, } fn fight(player_a: &mut ...
0 votes
2 answers
958 views
Modify multiple elements of a slice/collection at the same time [duplicate]
I have a Vec<Vec<T>> (I know it's not ideal; it's from a library). I want to look at pairs of Vecs in the outer vec and push to each of them, something like this: let mut foo = vec![ ...
3 votes
0 answers
2k views
How to modify each section of a vector in multiple threads [duplicate]
I have a vector of u8 and I need to fill this vector with values that can be computed in parallel: let vector: Vec<u8> = vec![0, len]; Given n_threads threads, each thread can take care of a ...
1 vote
1 answer
1k views
How to loop twice over the same vector? [duplicate]
I am trying to update elements of a vector knowing where each other is, i.e. test all pairs of elements (unordered), while changing them. So I started naively writing this: for x in &mut v { ...
1 vote
1 answer
491 views
In Rust can you have an immutable vector of mutable vectors? [duplicate]
Probably not the right question but trying to learn rust and wondering if there's a way to do this without having to copy out first. old example Updated example link I have a Vec<Vec> where the ...
0 votes
0 answers
423 views
How to update a value in a collection in a struct based on another value in the same collection? [duplicate]
The question says "a collection in a struct", but I'm uncertain whether the "in a struct" part is actually relevant. I may not understand the problem well enough to properly ask the question. In any ...
0 votes
0 answers
406 views
How I can get mutable element from Vec in Rust [duplicate]
I'm new to Rust and I'm trying to get Vec<i32> element in swap() function and change it: use std::ops::Index; fn main() { let mut vec = Vec::new(); for i in 0..10 { vec.push(i); ...
0 votes
2 answers
241 views
Mutable and immutable borrow problems with Vec<Vec<i32>> [duplicate]
I'm solving a problem from Leetcode and encountered the fact that Rust won't let me execute it efficiently. What am I doing wrong? I know about the book article about references and borrowing and ...
0 votes
0 answers
369 views
How to reorder the elements of an array in-place? [duplicate]
I would like to write a function fn f<A>(xs: &mut [A; 9]) that reorders an array in-place from: [a, b, c, d, e, f, g, h, i] to: [g, d, a, h, e, b, i, f, c] I can't reassign the array ...