Cell docs needs this simple example

I’ve been reading Cell docs a few times ending them with sentence… “maybe some other time i’ll get it…” and then i hit this

Such a simple example

use std::cell::Cell;

struct NaiveRc<'a, T: 'a> {
    inner_value: &'a T,
    references: Cell<usize>

let x = NaiveRc { inner_value: &1, references: Cell::new(1) }; // x is immutable
x.references.set(2); // it works!
x.inner_value = &2;  // beep boop, x is immutable,
                     // you can't assign a new value to any of its fields!

What do you think about including it as example?


I think that opening a PR that adds that as an example before the “see the module level documentation for more” on the cell struct would be great. The docs maintainers would respond yea or nay, and ask for changes if they felt like it needed it.

That exact example won’t work (since it won’t build), you’ll need to comment out the attempt to set the inner value.

Perfect. I did it here: https://github.com/rust-lang/rust/pull/43423 I changed example a bit because I think PR version is simpler then the blog version. Should I mention Ricardo Martins’ blog post somehow?

Thanks @quodlibetor

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.