Skip to content

Commit 6fa93e8

Browse files
Rollup merge of #118450 - marcin-serwin:master, r=workingjubilee
Use OnceCell in cell module documentation The spanning tree example in the std cell module implementation was created before `OnceCell` was added to Rust so it uses `RefCell`. However, in this case using `OnceCell` seems more appropriate and produces simpler code. As a bonus, this also means that all three cell types are presented in the examples of std cell module.
2 parents aa5f251 + 13c16e3 commit 6fa93e8

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

Diff for: library/core/src/cell.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,17 @@
143143
//!
144144
//! ```
145145
//! # #![allow(dead_code)]
146-
//! use std::cell::RefCell;
146+
//! use std::cell::OnceCell;
147147
//!
148148
//! struct Graph {
149149
//! edges: Vec<(i32, i32)>,
150-
//! span_tree_cache: RefCell<Option<Vec<(i32, i32)>>>
150+
//! span_tree_cache: OnceCell<Vec<(i32, i32)>>
151151
//! }
152152
//!
153153
//! impl Graph {
154154
//! fn minimum_spanning_tree(&self) -> Vec<(i32, i32)> {
155-
//! self.span_tree_cache.borrow_mut()
156-
//! .get_or_insert_with(|| self.calc_span_tree())
155+
//! self.span_tree_cache
156+
//! .get_or_init(|| self.calc_span_tree())
157157
//! .clone()
158158
//! }
159159
//!

0 commit comments

Comments
 (0)