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.
This commit is contained in:
Michael Goulet 2023-12-05 14:52:43 -05:00 committed by GitHub
commit 6fa93e83c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -143,17 +143,17 @@
//! //!
//! ``` //! ```
//! # #![allow(dead_code)] //! # #![allow(dead_code)]
//! use std::cell::RefCell; //! use std::cell::OnceCell;
//! //!
//! struct Graph { //! struct Graph {
//! edges: Vec<(i32, i32)>, //! edges: Vec<(i32, i32)>,
//! span_tree_cache: RefCell<Option<Vec<(i32, i32)>>> //! span_tree_cache: OnceCell<Vec<(i32, i32)>>
//! } //! }
//! //!
//! impl Graph { //! impl Graph {
//! fn minimum_spanning_tree(&self) -> Vec<(i32, i32)> { //! fn minimum_spanning_tree(&self) -> Vec<(i32, i32)> {
//! self.span_tree_cache.borrow_mut() //! self.span_tree_cache
//! .get_or_insert_with(|| self.calc_span_tree()) //! .get_or_init(|| self.calc_span_tree())
//! .clone() //! .clone()
//! } //! }
//! //!