Skip to content

Commit 5bd2852

Browse files
committed
feat: add refcell1 exercise
Signed-off-by: denton <[email protected]>
1 parent 88b583f commit 5bd2852

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// refcell1.rs
2+
//
3+
// Interior mutability is a design pattern in Rust that allows you to mutate
4+
// data even when there are immutable references to that data;
5+
// normally, this action is disallowed by the borrowing rules.
6+
7+
// The RefCell<T> type represents single ownership over the data it holds.
8+
// Recall the borrowing rules in Rust:
9+
// 1. At any given time, you can have either (but not both) one mutable
10+
// reference or any number of immutable references.
11+
// 2. References must always be valid.
12+
13+
// With references and Box<T>, the borrowing rules’ invariants are enforced at
14+
// compile time. With RefCell<T>, these invariants are enforced at runtime.
15+
// With references, if you break these rules, you’ll get a compiler error.
16+
// With RefCell<T>, if you break these rules, your program will panic and exit.
17+
// The RefCell<T> type is useful when you’re sure your code follows the
18+
// borrowing rules but the compiler is unable to understand and guarantee that.
19+
20+
// I AM NOT DONE
21+
22+
use std::cell::RefCell;
23+
24+
#[derive(Debug)]
25+
#[allow(dead_code)]
26+
struct User {
27+
name: RefCell<String>,
28+
}
29+
30+
#[allow(dead_code)]
31+
impl User {
32+
fn name(&self) -> String {
33+
self.name.borrow().to_string()
34+
}
35+
36+
// Note: do not use &mut self!
37+
fn set_name(&self, name: String) {
38+
todo!()
39+
}
40+
}
41+
42+
fn main() {
43+
let u = User {
44+
name: RefCell::new("Alice".to_string()),
45+
};
46+
println!("My name is {}!", *u.name.borrow());
47+
48+
let new_name = "Bob".to_string();
49+
u.set_name(new_name.clone());
50+
51+
println!("My name is {}!", *u.name.borrow());
52+
}

info.toml

+14
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,20 @@ Check out https://doc.rust-lang.org/std/borrow/enum.Cow.html for documentation
11061106
on the `Cow` type.
11071107
"""
11081108

1109+
[[exercises]]
1110+
name = "refcell1"
1111+
path = "exercises/19_smart_pointers/refcell1.rs"
1112+
mode = "compile"
1113+
hint = """
1114+
Remember that RefCell<T> allows for an immutable object to be modified.
1115+
1116+
Use the .borrow_mut() method on the RefCell to get a mutable reference to
1117+
the underlying data.
1118+
1119+
See https://doc.rust-lang.org/book/ch15-05-interior-mutability.html for more
1120+
information on RefCell.
1121+
"""
1122+
11091123
# THREADS
11101124

11111125
[[exercises]]

0 commit comments

Comments
 (0)