Questions tagged [refcell]
64 questions
2
votes
1 answer
Is there any way to borrow a RefCell immutably and mutably at the same time?
I have a piece of code which needs to operate on a list. This list contains items which come from another source and need to be processed and eventually removed. The list is also passed along to multiple functions which decide whether to add or…

Dandry
- 495
- 12
- 26
2
votes
1 answer
How to implement the std::hash::Hash trait on external data types in Rust?
I have a basic LinkedList implementation, where I want to iterate through my nodes, and add those nodes to a HashSet. I am unable to do this, however, because my nodes are wrapped in an Rc>>, and I'm having trouble implementing the…

modulitos
- 14,737
- 16
- 67
- 110
1
vote
1 answer
Borrow mutable inside of `match` arm
I'm new to Rust, so I'm sorry if this question has an absolutely obvious solution. I cannot find cleaner way to write the code below. I know this code cause panic because of container is already borrowed.
return match &container.borrow().parent {
…

Fanteria
- 62
- 1
- 4
1
vote
1 answer
Rust : drop(&RefMut) instead of drop(RefMut)?
I know when borrowing a value from a RefCell, I can manually drop it to end this borrow.
However, if I use the reference to a RefMut instead of directly using the RefMut, the drop trait seems invalid to end this borrow.
So, what happend when trying…

hnyls2002
- 103
- 8
1
vote
0 answers
Comparison between two Rc> - Unconsistent behavior
I'm playing with Rc> and came up with a behavior I don't understand.
Basically, in a struct, I own a collection of Rc>. I can push an item of type Concrete implementing Obj in the collection, and get a…

Antoine Morel
- 79
- 5
1
vote
0 answers
How to connect values inside two RefCells
Sample code :
use serde_json::{json, Value};
fn main() {
let a: Rc> = Rc::new(RefCell::new(json!({"a": "value"})));
let v: Rc>> = Rc::new(RefCell::new(vec![]));
// How to push a into v, but keep `a` and `v`…

Raywell
- 169
- 1
- 8
1
vote
3 answers
Rc/RefCell with parent of same struct
I'm trying to convert some object oriented code into Rust. It was going okay until I ran into this situation.
struct A {
root: Rc>,
}
struct B {
parent: Weak>,
c_lst: Vec
value: u32
}
struct C {
parent:…

Victor D
- 15
- 6
1
vote
1 answer
How do I implement an iterator from a vector of std::Rc> smart pointers?
I'm trying to understand how to work with interior mutability. This question is strongly related to my previous question.
I have a generic struct Port that owns a Vec. We can "chain" port B to port A so, when reading the content of port A, we…

Román Cárdenas
- 492
- 5
- 15
1
vote
1 answer
Mutating fields of Rc Refcell depending on its other internal fields
I need to to iterate over a field of a struct inside Rc RefCell and modify some of its arguments according to its other field.
For example for the struct Foo:
pub struct Foo {
pub foo1: Vec,
pub foo2: Vec,
}
The following code…

Adam
- 743
- 1
- 6
- 11
1
vote
1 answer
Immutable reference to data in RefCell
I'm trying to implement the Index method for a struct with interior mutability:
pub struct FooVec {
foo: RefCell>
}
impl Index for FooVec {
type Output = i32;
fn index(&self, index: usize) -> &Self::Output {
…

Adam
- 743
- 1
- 6
- 11
1
vote
1 answer
RefMut lifetime error when returning from closure
I have a RefCell in my structure which I want to access and modify. However, the only way I can access this RefCell is using a closure. Hence, I'd like to return a mutable reference (RefMut) from the closure which I can use and modify from the…

Albert Tomanek
- 387
- 4
- 13
1
vote
0 answers
Accessing struct member that is inside Option of a Rc in Rust
I want to change a struct member value inside a method that is of type Rc>.
Lets say I have this struct:
struct TestStruct {
pub vecfield: Rc>>,
}
impl TestStruct {
fn set_vec(&self) {
…

sh1nnen
- 11
- 1
1
vote
1 answer
How can I return a reference to the inner data of a RefCell
I'm creating a lazily loading struct:
pub struct LazyData T> {
data: RefCell

trexinf14s
- 261
- 2
- 13
1
vote
0 answers
Iterate over linked structs wrapped in Rc and update them
I need to implement the A* pathfinding algorithm in Rust. The algorithm processes particular cells and links with one another using a parent_cell field. In the end, when the destination cell is found, the path is the way from the destination cell to…

Dandry
- 495
- 12
- 26