Questions tagged [mutable-reference]
23 questions
16
votes
1 answer
Reborrowing of mutable reference
When I wondered how a mutable reference could move into a method, all the questions began.
let a = &mut x;
a.somemethod(); // value of a should have moved
a.anothermethod(); // but it works.
I've googled a lot. (really a lot)
And I've noticed that…

kwonryul
- 481
- 3
- 10
5
votes
1 answer
Rust borrow checker and early returns
Rust-lang Playground link
struct Foo {
val: i32
}
impl Foo {
pub fn maybe_get(&mut self) -> Option<&mut i32> {
Some(&mut self.val)
}
pub fn definitely_get(&mut self) -> &mut i32 {
{ // Add closure to ensure…

quittle
- 856
- 2
- 10
- 19
4
votes
1 answer
Rust function pointer seems to be treated as stateful by borrow checker
Following sample code doesn't compile:
fn invoke(i: i32, mut f: impl FnMut(i32)) {
f(i)
}
fn main() {
let f: fn(i32, _) = invoke;
let mut sum: i32 = 0;
for i in 0..10 {
_ = f(i, |x| sum += x);
}
println!("{:?}",…

google2
- 315
- 3
- 8
3
votes
1 answer
Why are mutable references inconsistent between impl and normal functions?
For functions in an impl block we use this syntax:
fn test(&mut self) {}
But for a normal function we use this syntax:
fn test(data: &mut u64) {}
I understand self is a variable, while Self is type. In first case we use &mut with the variable…

Siddharth Teli
- 87
- 6
2
votes
1 answer
Returning a mutable reference to a field of a mutably referenced struct
I am still quite new to the advanced topics in rust, but for context, I am trying to implement a generic quadtree in rust.
With the method find_mut(&mut self,x,y) I want to traverse the quadtree to find the lowermost subtree containing that…

RibsGrowBack
- 45
- 6
2
votes
1 answer
How to iterate over tuple of trait objects or unsized types
This question was originally found in this post in reddit.
Whilst experienced Rust user will spot out that the elements in a tuple does not have to be the same (if they are, you should use array!), and so it does not make sense to iterate through…

Earth Engine
- 10,048
- 5
- 48
- 78
1
vote
2 answers
Insert method for link list in rust
I am learning about link list & thought to write a simple one.
struct ListNode {
val: i32,
next: Option>,
}
with a impl block
impl ListNode {
fn new(val: i32) -> Self {
ListNode { next: None, val }
}
//…

Siddharth Teli
- 87
- 6
1
vote
1 answer
How can I deal efficiently with Rust's borrow checking rule of not being able to have 2 mutable references on 1 object?
I am currently in process of learning Rust by porting a Constructive Solid Geometry (CSG)
library and I have been wrestling with borrow checker and its' "no 2 mutable references on single object" policy.
In the CSG library, I have a function for…

Akufishi
- 11
- 2
1
vote
1 answer
Borrow errors for mutable refrence of struct
struct C {
p: String,
q: String,
}
impl C {
fn call(&mut self) {}
}
fn main(){
let mut c = C { p: "p".to_string(), q: "q".to_string() };
let p = &mut c.p; // first mutable borrow occurs here
let q = &mut c.q; //…

wangliqiu
- 369
- 1
- 9
1
vote
0 answers
Error 0499 when using mutable references in a for loop
I have 3 structs Player, Dealer and Cards. Dealer contains a vec of Cards whiles Player contains a vec of references to some of those cards that the dealer has.
pub struct Dealer{
cards: Vec,
}
pub struct Player<'a>{
…

Isaac Dzikum
- 184
- 9
1
vote
2 answers
Having hard time managing compounds of references and dereferences
let mut x = 1;
let a = &mut x;
let b = &mut *a;
*a = 2; // error. b borrows a
*b = 3; // it works! (only without " *a = 2 ")
let mut x = 1;
let b;
{
let a = &mut x;
b = &mut *a;
} // a drops here
*b = 2; // it works!
I'm having hard time…

kwonryul
- 481
- 3
- 10
1
vote
1 answer
'dataURI' variable from inside React Hook useEffect will be lost after each render
I would like to translate the function below written in vanilla javascript to a react function. The function below allows a user to click on the image input and append the image to the textarea which holds a class ".editor"
function getImage() {
…

user3574939
- 819
- 3
- 17
- 34
0
votes
1 answer
Rust Reborrowing Shared Reference from Mutable Reference
I'm new to Rust, and trying to understand why a mutable reference to a data structure that has had an element be borrowed can seemingly be used again within the same lifetime to borrow another element. This is namely in regards to an example from an…

mishar
- 89
- 6
0
votes
1 answer
How can I return a mutable reference to a value owned by the current function
Here is my struct and I need a get_mut function that returns a mutable reference to a value owned either by
ctx.scope or ctx.parent.scope recursively
pub type RuntimeContext<'c> = Rc>>;
pub struct Context<'c> {
pub parent:…

Sassy
- 40
- 4
0
votes
1 answer
How to assign a the result of `Command::arg()` to a field?
tokio's arg() returns a mutable reference to a Command. How can I assign it to a field?
pub struct Manager<'a> {
pub cmd: &'a mut tokio::process::Command
}
impl<'a> Manager<'a> {
pub fn new() -> Manager<'a> {
Manager {
…

Christopher Wilke
- 65
- 1
- 1
- 10