Skip to content

Commit

Permalink
Ref Cell
Browse files Browse the repository at this point in the history
  • Loading branch information
saltukalakus committed Dec 1, 2024
1 parent b7c89cf commit dc97b03
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@
- [Feature Attribute](./essentials/packaging/feature-attribute.md)
- [Features Compilation](./essentials/packaging/features-compilation.md)
- [Standard Library](./essentials/std-lib/intro.md)
- [self Keyword](./essentials/std-lib/self.md)
- [Ref Cell Type](./essentials/std-lib/refcell.md)
- [Self Type](./essentials/std-lib/self-as-type.md)
- [Self vs self](./essentials/std-lib/self-vs-self.md)
- [self Keyword](./essentials/std-lib/self.md)
- [imp Keyword](./essentials/std-lib/imp.md)
- [new Keyword](./essentials/std-lib/new.md)
- [Option Enum](./essentials/std-lib/option.md)
Expand Down
32 changes: 32 additions & 0 deletions src/essentials/std-lib/refcell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
### Understanding `RefCell` in Rust

`RefCell` is a type that provides interior mutability in Rust. It allows you to mutate data even when there are immutable references to that data. This is achieved by enforcing borrowing rules at runtime rather than compile time.

### Key Points

- `RefCell` is part of the `std::cell` module.
- It enables mutable borrowing checked at runtime.
- Useful in scenarios where you need to mutate data but only have an immutable reference.

#### Example

Here's a simple example to illustrate how `RefCell` works:

```rust
{{#include refcell/src/main.rs}}
```

### Explanation

1. We create a `RefCell` containing the value `5`.
2. We borrow the value immutably using `borrow()`.
3. We borrow the value mutably using `borrow_mut()` and modify it.
4. We borrow the value immutably again to verify the change.

### Important Notes

- `RefCell` will panic at runtime if you violate borrowing rules (e.g., if you try to borrow mutably while an immutable borrow is active).
- Use `RefCell` when you need interior mutability and are sure that the borrowing rules will be followed at runtime.
- `RefCell` works when the value is managed in a single thread. For multi-threaded scenarios use Mutex or RwLock.

For more details, refer to the [Rust documentation on `RefCell`](https://doc.rust-lang.org/std/cell/struct.RefCell.html).
7 changes: 7 additions & 0 deletions src/essentials/std-lib/refcell/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/essentials/std-lib/refcell/cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "std_lib_refcell"
version = "0.1.0"
edition = "2018"

[dependencies]
23 changes: 23 additions & 0 deletions src/essentials/std-lib/refcell/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::cell::RefCell;

fn main() {
let data = RefCell::new(5);

// Borrowing the value immutably
{
let value = data.borrow();
println!("Value: {}", *value);
}

// Borrowing the value mutably
{
let mut value = data.borrow_mut();
*value += 1;
}

// Borrowing the value immutably again to see the change
{
let value = data.borrow();
println!("Updated Value: {}", *value);
}
}

0 comments on commit dc97b03

Please sign in to comment.