-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
Add simple docs example for struct Cell #43423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -187,6 +187,29 @@ use ops::{Deref, DerefMut, CoerceUnsized}; | |
| use ptr; | ||
|
|
||
| /// A mutable memory location. | ||
| /// # Examples | ||
| /// Here you can see how using `Cell<T>` allows to use mutable field inside | ||
| /// immutable struct (which is also called 'interior mutability'). | ||
| /// ``` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also above this line |
||
| /// use std::cell::Cell; | ||
| /// | ||
| /// struct SomeStruct { | ||
| /// regular_field: u8, | ||
| /// special_field: Cell<u8>, | ||
| /// } | ||
| /// | ||
| /// let my_struct = SomeStruct { | ||
| /// regular_field: 0, | ||
| /// special_field: Cell::new(1), | ||
| /// }; | ||
| /// | ||
| /// let new_value = 100; | ||
| /// // ERROR, because my_struct is immutable | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also one above this line |
||
| /// // immutable.regular_field = new_value; | ||
| /// // WORKS, although `my_struct` is immutable, field `special_field` is mutable because it is Cell | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also one above this line |
||
| /// immutable.special_field.set(new_value); | ||
| /// assert_eq!(immutable.special_field.get(), new_value); | ||
| /// ``` | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this follows the prevailing style in the docs. For example, I think it is at least missing an
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added some prose and example section. Maybe it's just me but I was reading this https://doc.rust-lang.org/std/cell/ a few times and still didn't know what |
||
| /// | ||
| /// See the [module-level documentation](index.html) for more. | ||
| #[stable(feature = "rust1", since = "1.0.0")] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you put a
///above and below this lineThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
like here? 8286075
Because i put it at first place and it was the cause of CI failure (something related to tidy check)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes. you had trailing whitespace; that was the only issue, not the
///s. In other wordsnot
make sense?