Skip to content

Commit 9abc936

Browse files
authored
Rollup merge of #61029 - blkerby:minimum_spanning_tree, r=alexcrichton
Simplify RefCell minimum_spanning_tree example This simplifies the implementation of the `minimum_spanning_tree` example of `RefCell` in the `cell` module-level docs, avoiding an unnecessary recursive call. This also eliminates the need for a block to contain the scope of the borrow in this example. But since that use of a block served an important didactic purpose, we make up for this by instead introducing a block in the initial, simpler example of `RefCell`, where the point will hopefully be conveyed to the reader more easily.
2 parents fba5ed3 + e641fb4 commit 9abc936

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

src/libcore/cell.rs

+22-24
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,26 @@
6767
//! mutability:
6868
//!
6969
//! ```
70+
//! use std::cell::{RefCell, RefMut};
7071
//! use std::collections::HashMap;
71-
//! use std::cell::RefCell;
7272
//! use std::rc::Rc;
7373
//!
7474
//! fn main() {
7575
//! let shared_map: Rc<RefCell<_>> = Rc::new(RefCell::new(HashMap::new()));
76-
//! shared_map.borrow_mut().insert("africa", 92388);
77-
//! shared_map.borrow_mut().insert("kyoto", 11837);
78-
//! shared_map.borrow_mut().insert("piccadilly", 11826);
79-
//! shared_map.borrow_mut().insert("marbles", 38);
76+
//! // Create a new block to limit the scope of the dynamic borrow
77+
//! {
78+
//! let mut map: RefMut<_> = shared_map.borrow_mut();
79+
//! map.insert("africa", 92388);
80+
//! map.insert("kyoto", 11837);
81+
//! map.insert("piccadilly", 11826);
82+
//! map.insert("marbles", 38);
83+
//! }
84+
//!
85+
//! // Note that if we had not let the previous borrow of the cache fall out
86+
//! // of scope then the subsequent borrow would cause a dynamic thread panic.
87+
//! // This is the major hazard of using `RefCell`.
88+
//! let total: i32 = shared_map.borrow().values().sum();
89+
//! println!("{}", total);
8090
//! }
8191
//! ```
8292
//!
@@ -102,27 +112,15 @@
102112
//!
103113
//! impl Graph {
104114
//! fn minimum_spanning_tree(&self) -> Vec<(i32, i32)> {
105-
//! // Create a new scope to contain the lifetime of the
106-
//! // dynamic borrow
107-
//! {
108-
//! // Take a reference to the inside of cache cell
109-
//! let mut cache = self.span_tree_cache.borrow_mut();
110-
//! if cache.is_some() {
111-
//! return cache.as_ref().unwrap().clone();
112-
//! }
113-
//!
114-
//! let span_tree = self.calc_span_tree();
115-
//! *cache = Some(span_tree);
116-
//! }
115+
//! self.span_tree_cache.borrow_mut()
116+
//! .get_or_insert_with(|| self.calc_span_tree())
117+
//! .clone()
118+
//! }
117119
//!
118-
//! // Recursive call to return the just-cached value.
119-
//! // Note that if we had not let the previous borrow
120-
//! // of the cache fall out of scope then the subsequent
121-
//! // recursive borrow would cause a dynamic thread panic.
122-
//! // This is the major hazard of using `RefCell`.
123-
//! self.minimum_spanning_tree()
120+
//! fn calc_span_tree(&self) -> Vec<(i32, i32)> {
121+
//! // Expensive computation goes here
122+
//! vec![]
124123
//! }
125-
//! # fn calc_span_tree(&self) -> Vec<(i32, i32)> { vec![] }
126124
//! }
127125
//! ```
128126
//!

0 commit comments

Comments
 (0)