Questions tagged [refcell]
64 questions
0
votes
1 answer
Peekable content is None after refactor the code
I have this method:
fn parse_http_request_headers<'b>(sp: &'b mut std::str::Split<&str>) -> HashMap {
let mut headers: HashMap = HashMap::new();
let mut iter = sp.peekable();
while iter.peek()…

Alex Vergara
- 1,766
- 1
- 10
- 29
0
votes
0 answers
Pattern matching a mutable Rc> without a method
Suppose I have the following code:
enum Foo {
X { x: i32 },
Y { y: String },
}
// Change x value if foo is X, otherwise do nothing.
fn test(foo: Rc>) {
panic!("help!")
}
How do I implement test? I know I can implement it…

Gal
- 5,338
- 5
- 33
- 55
0
votes
1 answer
rust two objects refering each borrow_mut pattern
A similar question I posted earlier is here
Rust can't modify RefCell in Rc, but completely different.
I want to simulate some natural process, so I have a Simulator, and a reactor like a NuclearReactor. The simulator will modify the reactor, and…

YNX
- 511
- 6
- 17
0
votes
1 answer
Returning iterator from weak references for mapping and modifying values
I'm trying quite complex stuff with Rust where I need the following attributes, and am fighting the compiler.
Object which itself lives from start to finish of application, however, where internal maps/vectors could be modified during application…

tradinggy
- 1,211
- 1
- 8
- 6
0
votes
1 answer
Implement Borrow on something behind a RefCell?
I have the structs Value and RefValue in my project. RefValue is a reference-counted, dynamically borrowable Value. Now Value may contain a HashMap of RefValue, where both the key and the value is a RefValue.
type ValueMap = HashMap

glasflügel
- 325
- 2
- 11
0
votes
0 answers
Rust - storing a temporary "context" - lifetimes vs smart pointers
I am trying to learn Rust, specifically lifetimes and smart pointers and would like to know the most Rusty way of doing this.
Specifically, assume we are designing a mock language and "analyzer":
a=1 # each line is of form name=number|name
b=2
c=a #…

Megh Parikh
- 924
- 2
- 7
- 25
0
votes
2 answers
refcell rc> doesn't change
Here is my simplified code. I need to change the pdp_state in a function. but the state remain 'a'. I don't figure out why cloning Rc does not work. I also checked this cloning out side a structure an it worked.
#[derive(Clone,Copy)]
enum…

Farbod PM
- 122
- 6
0
votes
1 answer
Access two mutable references from a Global hashmap at once in Rust
Say we have a globally accessible hashmap of trait objects we make with lazy_static: MY_ANIMALS: Mutex>, where type AnimalBox = Box
Now, we want the animals in this global hashmap to interact with each…

ANimator120
- 2,556
- 1
- 20
- 52
0
votes
0 answers
How can I modify a reference to a field within the same struct using Rc and RefCell?
I have a struct with a field that references the current element on an array in the same struct using Rc. I can now have a reference to any element from then nums field:
use std::{cell::RefCell, rc::Rc};
struct ArrayTest {
nums: [u32; 8],
…
user12513149
0
votes
2 answers
Is RefCell an appropriate workaround to borrow two mutable elements from a vector?
Consider this toy example of "fighting" two random "players":
#[derive(Clone)]
struct Player {
name: String,
health: i32,
attack: i32,
}
fn fight(player_a: &mut Player, player_b: &mut Player) {
player_a.health -= player_b.attack;
…

t_d_milan
- 55
- 3
0
votes
0 answers
Rust panics when I inline borrow on RefCell<>
When I run the following line in a function, Rust panics. X is &Tree and Tree is Rc>>. TreeNode is a struct.
x.borrow_mut().parent.as_ref().unwrap().borrow_mut().left = Option::from(y.as_ref().unwrap().clone());
I tried changing…

Jay
- 15
- 4
0
votes
1 answer
"cannot return value referencing temporary value" and interior mutability in Rust
I have the following code in Rust:
pub struct RegExpFilter {
...
regexp_data: RefCell

4ntoine
- 19,816
- 21
- 96
- 220
0
votes
1 answer
Passing around a reference to a struct in Rust
My problem basically is that in my program I need to pass around the reference to the s struct to multiple places including a new thread. In C I could for example declare it a a global struct and use it that way.
How can I do this in rust?
I also…

Andrzej C
- 25
- 5
0
votes
1 answer
How to create a value with reference-counted references to itself while checking an already-borrowed field?
I'm trying to create a mutable structure B which stores instances of other structure A that hold references to B. I want an implementation such that any mutation done to the original B propagates to the references held in the As.
However, during the…

DrunkCoder
- 379
- 2
- 9
0
votes
1 answer
Error borrowing content while rewriting a function to use traits
I'm new to Rust and toy with the language a little bit.
I wrote two simple functions that increment and decrements a value inside Rc>. Since this functionality makes sense for any number type, I tried to turn them into generic…

Natjo
- 2,005
- 29
- 75