interior-mutability

How can I use internal mutability with generic type in Rust?

家住魔仙堡 提交于 2021-01-27 11:43:11
问题 I would like to design a struct in Rust that can be constructed with an object that implements the Digest trait, and abstract the behavior of the hash behind a method. Here's a simple example that doesn't compile: use digest::Digest; struct Crypto<D: Digest> { digest: D, } impl<D> Crypto<D> where D: Digest, { pub fn hash(&self, data: &[u8]) -> Vec<u8> { self.digest.chain(&data).finalize_reset().to_vec() } } This fails to compile because self is immutably borrowed in the method signature, so

How to access value in RefCell properly

老子叫甜甜 提交于 2020-05-27 04:43:00
问题 I'm trying to wrap my head around Rc and RefCell in Rust. What I'm trying to achieve is to to have multiple mutable references to the same objects. I came up with this dummy code: use std::rc::Rc; use std::cell::RefCell; struct Person { name: String, mother: Option<Rc<RefCell<Person>>>, father: Option<Rc<RefCell<Person>>>, partner: Option<Rc<RefCell<Person>>> } pub fn main () { let mut susan = Person { name: "Susan".to_string(), mother: None, father: None, partner: None }; let mut boxed_susan

How to access value in RefCell properly

牧云@^-^@ 提交于 2020-05-27 04:42:31
问题 I'm trying to wrap my head around Rc and RefCell in Rust. What I'm trying to achieve is to to have multiple mutable references to the same objects. I came up with this dummy code: use std::rc::Rc; use std::cell::RefCell; struct Person { name: String, mother: Option<Rc<RefCell<Person>>>, father: Option<Rc<RefCell<Person>>>, partner: Option<Rc<RefCell<Person>>> } pub fn main () { let mut susan = Person { name: "Susan".to_string(), mother: None, father: None, partner: None }; let mut boxed_susan

Is there a way to make an immutable reference mutable?

一曲冷凌霜 提交于 2020-01-14 03:46:07
问题 I want to solve a leetcode question in Rust (Remove Nth Node From End of List). My solution uses two pointers to find the Node to remove: #[derive(PartialEq, Eq, Debug)] pub struct ListNode { pub val: i32, pub next: Option<Box<ListNode>>, } impl ListNode { #[inline] fn new(val: i32) -> Self { ListNode { next: None, val } } } // two-pointer sliding window impl Solution { pub fn remove_nth_from_end(head: Option<Box<ListNode>>, n: i32) -> Option<Box<ListNode>> { let mut dummy_head = Some(Box:

Situations where Cell or RefCell is the best choice

女生的网名这么多〃 提交于 2019-12-28 01:27:30
问题 When would you be required to use Cell or RefCell? It seems like there are many other type choices that would be suitable in place of these, and the documentation warns that using RefCell is a bit of a "last resort". Is using these types a "code smell"? Can anyone show an example where using these types makes more sense than using another type, such as Rc or even Box ? 回答1: It is not entirely correct to ask when Cell or RefCell should be used over Box and Rc because these types solve

Cyclic reference of RefCell borrows in traversal

余生颓废 提交于 2019-12-13 16:07:30
问题 I'm learning Rust and tried coding a doubly-linked list. However, I'm stuck already at a typical iterative traversal implementation. I'm getting the impression that the borrow checker / drop checker is too strict and cannot infer the correct lifetime for the borrow when it crosses the function boundary from RefCell . I need to repeatedly set a variable binding ( curr in this case) to the borrow of its current contents: use std::cell::RefCell; use std::rc::Rc; pub struct LinkedList<T> { head:

Returning reference from RefCell [duplicate]

旧巷老猫 提交于 2019-12-11 08:40:02
问题 This question already has answers here : How do I borrow a RefCell<HashMap>, find a key, and return a reference to the result? [duplicate] (1 answer) How do I return a reference to something inside a RefCell without breaking encapsulation? (3 answers) Closed last year . Why does this program not compile use std::cell::RefCell; struct S { field: RefCell<String>, } impl S { fn take_ref(&self) -> &str { &self.field.borrow() } } fn main() { let s = S { field: RefCell::new("abc".to_string()), }; }

Is there an alternative or way to have Rc<RefCell<X>> that restricts mutability of X?

孤街醉人 提交于 2019-12-10 13:43:29
问题 For example given this code: use std::rc::Rc; use std::cell::RefCell; // Don't want to copy for performance reasons struct LibraryData { // Fields ... } // Creates and mutates data field in methods struct LibraryStruct { // Only LibraryStruct should have mutable access to this data: Rc<RefCell<LibraryData>> } impl LibraryStruct { pub fn data(&self) -> Rc<RefCell<LibraryData>> { self.data.clone() } } // Receives data field from LibraryStruct.data() struct A { data: Rc<RefCell<LibraryData>> }