Linked Questions

9 votes
3 answers
4k views

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: &...
tronje's user avatar
  • 93
11 votes
1 answer
4k views

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 ...
David Edmonds's user avatar
3 votes
2 answers
3k views

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>>,...
franza's user avatar
  • 2,327
2 votes
1 answer
2k views

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 ...
Adam's user avatar
  • 915
1 vote
2 answers
2k views

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]); ...
user2655800's user avatar
1 vote
1 answer
757 views

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 ...
user3169543's user avatar
  • 1,609
0 votes
2 answers
448 views

Consider this toy example of "fighting" two random "players": #[derive(Clone)] struct Player { name: String, health: i32, attack: i32, } fn fight(player_a: &mut ...
t_d_milan's user avatar
0 votes
2 answers
958 views

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![ ...
igneous_flock's user avatar
3 votes
0 answers
2k views

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 ...
Nick's user avatar
  • 10.6k
1 vote
1 answer
1k views

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 { ...
Creart's user avatar
  • 146
1 vote
1 answer
491 views

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 ...
MrPurpleStreak's user avatar
0 votes
0 answers
423 views

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 ...
VeryCasual's user avatar
0 votes
0 answers
406 views

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); ...
Mhs's user avatar
  • 1
0 votes
2 answers
241 views

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 ...
mxkmn's user avatar
  • 652
0 votes
0 answers
369 views

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 ...
puritii's user avatar
  • 1,359

15 30 50 per page