Questions tagged [vec]
78 questions
-1
votes
1 answer
How to slice to a particular element in a Vec?
What is the best way to slice a Vec to the first occurrence of a particular element?
A naive method demonstrating what I want to do:
fn main() {
let v = vec![1, 2, 3, 4, 5, 6];
let to_num = 5;
let mut idx = 0;
for x in &v {
…

belkarx
- 202
- 4
- 15
-1
votes
1 answer
How does Vec implement BorrowMut?
I ran the following example in the standard library documentation, and there was a puzzle.
I found an implementation of the BorrowMut trait with Vec,
I don't understand how it works. For example, where the code below indicates that No.1 works, why…

XiaoY
- 9
-3
votes
1 answer
Borrow error when I try to modify last element of a vector
I am trying to modify a Vec by doing thing[thing.len() - 1] = other_thing;. I get an error telling me that I borrowed first a mutable reference, then an immutable one. I could not find a workaround. How to modify last element of a vector in…

Preons
- 9
- 2