Questions tagged [refcell]
64 questions
19
votes
2 answers
Understanding usage of Rc> in Rust
I'm looking at some code that uses
Rc>
So I went out to read about the differences between Rc and RefCell:
Here is a recap of the reasons to choose Box, Rc, or RefCell:
Rc enables multiple owners of the same data; Box and…

Guerlando OCs
- 1,886
- 9
- 61
- 150
7
votes
2 answers
Rust, how to copy the inner value out from Rc> and return it?
Brief: I'm new to Rust so I decided to practice by implementing a double linked list. For debugging purpose, I implemented the get() method, but I failed to copy the value out from a Rc>. (Sorry for asking dumb question)
Problem: I'm…

Saplyn
- 83
- 3
7
votes
4 answers
How to return an *optional* reference into RefCell contents
I have a type that stores its data in a container behind a Rc>, which is for the most part hidden from the public API. For example:
struct Value;
struct Container {
storage: Rc>>,
}
impl Container {
fn…

user4815162342
- 141,790
- 18
- 296
- 355
6
votes
0 answers
How do I consume the inner value of a Rc>>?
I have a function of a third party library that needs ownership of a variable. Unfortunately this variable is inside a Rc>>.
My code looks simplified like this:
use std::cell::RefCell;
use std::rc::Rc;
pub struct Foo {
val:…

Natjo
- 2,005
- 29
- 75
4
votes
2 answers
How can I implement Deref for a struct that holds an Rc>?
My goal is to delegate method calls against my struct to a Trait's methods, where the Trait object is inside an Rc of RefCell.
I tried to follow the advice from this question:
How can I obtain an &A reference from a Rc>?
I get a compile…

Paul Chernoch
- 5,275
- 3
- 52
- 73
3
votes
1 answer
future created by async block is not `Send`
I do a server update in rust. it create patches between 2 binaries files, and serves static files
I try to do
let mut update_state;
if let Some(state) = update_stream.next().await {
if let Ok(state) = state {
update_state…

Vana
- 753
- 3
- 11
- 20
3
votes
1 answer
Rust: How to return a reference to an Rc> value?
I'm trying to learn Rust and I'm having some problems with different smart pointers and stuff.
Here is my code:
pub struct MyMap {
map: Rc>>,
}
impl MyMap {
// Not entire sure if it's supposed to be…

Gal
- 5,338
- 5
- 33
- 55
3
votes
2 answers
RefMut borrowed from Option does not live long enough (Option>>)
First of all, I apologize if this question has been asked before. The only similar problem I could find was this (but it seems different) :
Cyclic reference does not live long enough
My code is :
use std::cell::RefCell;
use std::rc::Rc;
type…

rostyslav52
- 53
- 5
3
votes
2 answers
How to handle "temporary value dropped" error when adapting Box-based tree structure to Rc+RefCell?
I've created a tree with a type definition similar to:
#[derive(Debug, Clone)]
pub(crate) struct TreeBox {
root: Option>>,
}
#[derive(Debug, Clone)]
struct NodeBox {
value: T,
left: Option>>,
…

cl_um
- 33
- 4
2
votes
1 answer
Hash trait does not work for Rc> in enum
I define a struct MyData and implement PartialEq and Hash traits for it manually.
I define a enum which includes Rc and Rc>.
I want derive PartialEq and Hash for the enum, but fails:
The PartialEq and Hash both work for…

Bingzheng Wu
- 435
- 3
- 11
2
votes
1 answer
How to build a pool of mutable Vecs that get reused on Drop?
I am trying to create a pool of mutable Vec objects that can be passed out to functions as needed, and reused when they're no longer needed (Since my target is WASM, I don't want to let Vecs themselves deallocate and reallocate). I have an…

Jason Siefken
- 737
- 7
- 12
2
votes
1 answer
Return Ref to something inside of Rc> without Ref::map
Working code first:
use std::cell::{Ref, RefCell};
use std::rc::Rc;
struct ValueHolder {
value: i32
}
fn give_value(wrapped: &Rc>) -> Ref {
Ref::map(
(**wrapped).borrow(),
|borrowed| {…

Harald Meisner
- 127
- 4
2
votes
1 answer
Why does RefCell:borrow_mut result in a BorrowMutError when used on both sides of a short-circuiting boolean AND (&&)?
I wrote this code for the leetcode same tree problem:
use std::cell::RefCell;
use std::rc::Rc;
// Definition for a binary tree node.
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left:…

Shuumatsu
- 592
- 2
- 5
- 12
2
votes
1 answer
Handle downcast error when downcasting Ref> into Ref>
I need to write a function foo that takes a &RefCell>, borrows from the RefCell and returns a downcasted object. The downcasted type is chosen at runtime, but for this example let's assume it's usize.
use core::any::Any;
use…

Gabriel Mutti
- 23
- 4
2
votes
0 answers
Can an element of a RefCell> stored in a struct modify values of another element in the same Vec?
I have a struct containing a vector whose elements must be able to modify other elements in the vector.
I tried doing it like this:
use core::cell::RefCell;
struct Go {
car: i32,
}
impl Go {
pub fn attack(&mut self, val: &mut Self) {
…

Javid Farhan
- 21
- 4