Skip to content

improve documentation of RefCell::get_mut #40203

@comex

Description

@comex
Contributor

From IRC:

<bletch>	how do I set the value of a RefCell? all the examples I see are about mutating it, not setting the value
<Mutabah>	You can overwrite the existing value if you can mutate
<Mutabah>	Just by doing `*cell.borrow_mut() = new_value`
<bletch>	but then it says it's an immutable borrowed content
<bletch>	when inside TLS
<bletch>	"cannot borrow immutable borrowed content as mutable"
<bletch>	with(|f| *f.get_mut() = new_gc)
[..]
<bletch>	ok, borrow_mut fixed it
<bletch>	yeah, I was using get_mut
<bletch>	I don't know why they're different

The documentation for get_mut is not terrible:

Returns a mutable reference to the underlying data.
This call borrows RefCell mutably (at compile-time) so there is no need for dynamic checks.

but I think it could be more explicit that get_mut is for special circumstances / usually not what you want. The name get_mut may tend to confuse newbies since it looks 'basic' and very similar to borrow_mut.

Activity

added
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
and removed
A-docsArea: Documentation for any part of the project, including the compiler, standard library, and tools
on Mar 10, 2017
steveklabnik

steveklabnik commented on Mar 15, 2017

@steveklabnik
Contributor

doc team triage: p-medium

GuillaumeGomez

GuillaumeGomez commented on Mar 18, 2017

@GuillaumeGomez
Member

Took a look at it and the prototype of the method seems clear enough to me:

fn get_mut(&mut self) -> &mut T

It clearly expects self to be mutable. However, I'll add something about it to be clear but I don't think this is really necessary...

added a commit that references this issue on Apr 24, 2017
63c7721
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsP-mediumMedium priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @steveklabnik@comex@GuillaumeGomez

        Issue actions

          improve documentation of RefCell::get_mut · Issue #40203 · rust-lang/rust