-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b7c89cf
commit dc97b03
Showing
5 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |