Why have I never comitted this?

This commit is contained in:
Fabian Schmidt 2025-03-05 19:44:46 +01:00
parent 8ed8e6e50d
commit 1eb1add888
3 changed files with 124 additions and 0 deletions

View File

@ -0,0 +1,55 @@
// https://www.youtube.com/watch?v=HwupNf9iCJk
use std::cell::{Cell, RefCell};
#[derive(Debug)]
// Could simplify this with Rc (see thread safe example)
struct Node<'a> {
// Cell for copyable values
val: Cell<i32>,
// RefCell probably better as there is no copying but can panic
str_val: RefCell<String>,
adjacent: Vec<&'a Node<'a>>,
}
fn add_one(node: &Node) {
let curr_val = node.val.get();
node.val.set(curr_val + 1);
for adj in node.adjacent.iter() {
add_one(adj);
}
}
fn add_urgency(node: &Node) {
node.str_val.borrow_mut().push('!');
for adj in node.adjacent.iter() {
add_urgency(adj);
}
}
fn main() {
let a = Node {
val: Cell::new(1),
str_val: RefCell::new(String::from("aaa")),
adjacent: vec![],
};
let b = Node {
val: Cell::new(2),
str_val: RefCell::new(String::from("bbb")),
adjacent: vec![&a],
};
let c = Node {
val: Cell::new(4),
str_val: RefCell::new(String::from("ccc")),
adjacent: vec![&a],
};
dbg!(&a);
dbg!(&b);
dbg!(&c);
add_one(&b);
dbg!(&b);
add_urgency(&c);
dbg!(&c);
}

19
src/bin/move.rs Normal file
View File

@ -0,0 +1,19 @@
// https://www.youtube.com/watch?v=MDvImhzrWr4
fn c<F: FnOnce() + 'static>(f: F) {
f();
}
fn main() {
let mut v = vec![1, 2, 3];
// use move keyword to get around 'static lifetime issue
c(move || {
v.push(4);
}); // without 'static closure lifetime ends here and we have no borrow issue
// cannot use v after moving the value
// this is where rc and refcell come into play
//v.push(5);
//dbg!(v);
}

View File

@ -0,0 +1,50 @@
// https://www.youtube.com/watch?v=HwupNf9iCJk
use std::{
sync::{Arc, RwLock},
thread,
};
#[derive(Debug)]
struct Node {
// RwLock similar to RefCell but thread safe,
// can also use Mutex and then .lock() it instead of calling .read() or .write()
str_val: RwLock<String>,
adjacent: Vec<Arc<Node>>,
}
fn add_urgency(node: Arc<Node>) {
{
node.str_val.write().unwrap().push('!');
}
for adj in node.adjacent.iter() {
add_urgency(adj.clone());
}
}
fn main() {
let a = Arc::new(Node {
str_val: RwLock::new(String::from("aaa")),
adjacent: vec![],
});
let b = Arc::new(Node {
str_val: RwLock::new(String::from("bbb")),
adjacent: vec![a.clone()],
});
let c = Arc::new(Node {
str_val: RwLock::new(String::from("ccc")),
adjacent: vec![a.clone()],
});
dbg!(a.clone());
dbg!(b.clone());
dbg!(c.clone());
let t1_b = b.clone();
let _t1 = thread::spawn(move || add_urgency(t1_b)).join();
dbg!(c.clone());
let t2_c = c.clone();
let _t2 = thread::spawn(move || add_urgency(t2_c)).join();
dbg!(c.clone());
}