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. |
| /// // 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.
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.
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.. .
steveklabnik
left a comment
There was a problem hiding this comment.
A few nits, and the build is failing.
|
|
||
| /// A mutable memory location. | ||
| /// | ||
| /// # Example |
There was a problem hiding this comment.
this should be Examples even though there's only one, for consistency
| /// | ||
| /// # Example | ||
| /// | ||
| /// Here you can see how using `Cell<T>` allows to use muttable field inside |
There was a problem hiding this comment.
mutable, not muttable
| /// # 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.
single quotes here please!
|
@steveklabnik done |
steveklabnik
left a comment
There was a problem hiding this comment.
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.
| use ptr; | ||
|
|
||
| /// A mutable memory location. | ||
| /// # Examples |
There was a problem hiding this comment.
can you put a /// above and below this line
There was a problem hiding this comment.
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.
yes. you had trailing whitespace; that was the only issue, not the ///s. In other words
"///"
not
"/// "
make sense?
| /// # Examples | ||
| /// Here you can see how using `Cell<T>` allows to use mutable field inside | ||
| /// immutable struct (which is also called 'interior mutability'). | ||
| /// ``` |
| /// 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.
also one above this line
| /// }; | ||
| /// | ||
| /// let new_value = 100; | ||
| /// // ERROR, because my_struct is immutable |
There was a problem hiding this comment.
also one above this line
There was a problem hiding this comment.
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.
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 ! |
|
📌 Commit d429a4e has been approved by |
|
@steveklabnik you are welcome :) |
Add simple docs example for struct Cell
No description provided.