-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add simple docs example for struct Cell #43423
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @BurntSushi (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
src/libcore/cell.rs
Outdated
/// // WORKS, special_field is mutable because it is Cell | ||
/// immutable.special_field.set(new_value); | ||
/// assert_eq!(immutable.special_field.get(), new_value); | ||
/// ``` |
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.
I don't think this follows the prevailing style in the docs. For example, I think it is at least missing an Examples
header. I'd also like to see some prose describing what the example is trying to show.
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.
I've added some prose and example section.
BTW: I'd see reference to this blog post:
https://ricardomartins.cc/2016/06/08/interior-mutability
in the docs. but perhaps adding links to blog post is not the way of doing docs. :)
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 Cell
is all about then read the blog post once to get the concept easily.. .
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.
A few nits, and the build is failing.
src/libcore/cell.rs
Outdated
@@ -187,6 +187,34 @@ use ops::{Deref, DerefMut, CoerceUnsized}; | |||
use ptr; | |||
|
|||
/// A mutable memory location. | |||
/// | |||
/// # Example |
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.
this should be Examples
even though there's only one, for consistency
src/libcore/cell.rs
Outdated
/// | ||
/// # Example | ||
/// | ||
/// Here you can see how using `Cell<T>` allows to use muttable field inside |
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.
mutable, not muttable
src/libcore/cell.rs
Outdated
/// # Example | ||
/// | ||
/// Here you can see how using `Cell<T>` allows to use muttable field inside | ||
/// immutable struct (which is also called "interior mutability"). |
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.
single quotes here please!
@steveklabnik done |
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.
sorry! just some last-minute white space changes; I must have missed them before?
This is good to go once that's fixed; I want to make sure this is consistent with the rest of the standard library.
src/libcore/cell.rs
Outdated
@@ -187,6 +187,29 @@ use ops::{Deref, DerefMut, CoerceUnsized}; | |||
use ptr; | |||
|
|||
/// A mutable memory location. | |||
/// # Examples |
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 line
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.
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 words
"///"
not
"/// "
make sense?
src/libcore/cell.rs
Outdated
/// # Examples | ||
/// Here you can see how using `Cell<T>` allows to use mutable field inside | ||
/// immutable struct (which is also called 'interior mutability'). | ||
/// ``` |
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.
also above this line
src/libcore/cell.rs
Outdated
/// let new_value = 100; | ||
/// // ERROR, because my_struct is immutable | ||
/// // immutable.regular_field = new_value; | ||
/// // WORKS, although `my_struct` is immutable, field `special_field` is mutable because it is Cell |
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.
also one above this line
src/libcore/cell.rs
Outdated
/// }; | ||
/// | ||
/// let new_value = 100; | ||
/// // ERROR, because my_struct is immutable |
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.
also one above this line
src/libcore/cell.rs
Outdated
/// // immutable.regular_field = new_value; | ||
/// | ||
/// // WORKS, although `my_struct` is immutable, field `special_field` is mutable because it is Cell | ||
/// immutable.special_field.set(new_value); |
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.
main isn't the issue. here's the error
01:00:45] error[E0425]: cannot find value `immutable` in this scope
[01:00:45] --> <anon>:22:1
[01:00:45] |
[01:00:45] 22 | immutable.special_field.set(new_value);
[01:00:45] | ^^^^^^^^^ not found in this scope
[01:00:45]
[01:00:45] error[E0425]: cannot find value `immutable` in this scope
[01:00:45] --> <anon>:23:12
[01:00:45] |
[01:00:45] 23 | assert_eq!(immutable.special_field.get(), new_value);
[01:00:45] | ^^^^^^^^^ not found in this scope
[01:00:45]
on this line, you use something called "immutable" but you never defined it; it's called my_struct
above
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.
You're right.. maybe i should go to sleep for today :)
r? @steveklabnik is it ok, what do you think? |
friendly ping for review @steveklabnik ! |
@bors: r+ rollup @xliiv thanks a ton! Sorry for the delays again |
📌 Commit d429a4e has been approved by |
@steveklabnik you are welcome :) |
Add simple docs example for struct Cell
No description provided.