Questions tagged [mutable-reference]

23 questions
0
votes
0 answers

Rust iterator with muatble reference keeps blocking other references after being used

I am giving a mutable reference of a Vec to an Iterator, so it can add Elements to it while just keeping an index of the Element. Thus after the Iterator being "used" shouldn't then the Vec be able to be referenced again? (Here a link to a Rust…
Emmmmllll
  • 21
  • 2
0
votes
1 answer

Pop function for a stack

I wanted to implement a stack, but I am having a lot of trouble with pop. I'm trying to do it with a while let loop, but can't seem to beat the borrow checker. pub struct Stack{ top: Option>>, } struct Node{ val: T, …
0
votes
1 answer

How do I match on a struct field in order to mutate it in place?

I am trying to mutate VecDeque inside a struct. I want to receive an event and delete it from VecDeque. pub struct Timeline { pub event_loop: Option> } impl Timeline { pub fn get_event(&mut self)-> Option { …
alpinemomo
  • 13
  • 1
0
votes
2 answers

Get mutable reference to an item in trait objects collection

To play around with Rust, I'm trying to get the following code working (playground, please don't mind the commented blocks, they are for further investigations). Basically I would like to store in a collection several items of several types…
0
votes
0 answers

Can I have a mutable reference to a type and its trait object in the same scope?

Can I have a mutable reference to a value and a mutable reference to a trait object of the same value inside the same scope? Is that undefined behavior? An example code snippet is added below for clarification. In the below code is it valid to have…
0
votes
1 answer

How can I apply an arbitrary in-place operation to a &mut reference?

Is there a way to write this function without requiring AddAssign or Clone on T? use std::ops::Add; fn increment>(x: &mut T) { *x = *x + 1; } As written, I get the error: error[E0507]: cannot move out of `*x` which is…
dspyz
  • 5,280
  • 2
  • 25
  • 63
0
votes
0 answers

Is there a clean way in Rust to mutate a `&mut` by replacing its value?

Is there a clean way to implement a safe function g that mutates a mutable reference by applying f on its value without having to implement Clone or Default (or any other special trait) for T? If not, why is or should this not be possible? Imagine a…
-1
votes
1 answer

Rust linked list cannot borrow previous and next elemt as mutable (just need immutable reference)

Currently, I'm working on a little application to simulate (multiple chained) pendulums. To save them I decided to go for a std::collections::LinkedList. Displaying them and moving them statically is not a problem, but for calculating the real…
Dzenan
  • 43
  • 2
  • 6
1
2