aboutsummaryrefslogtreecommitdiff
path: root/2022/rust
diff options
context:
space:
mode:
authorj-james2022-12-10 22:20:22 +0000
committerj-james2022-12-10 22:20:22 +0000
commitd2c93f846797fb5396c02a42d2f7ff9867a7acca (patch)
treeb5ee500bfb11e4e25aeab2bd2b997d717ca5abeb /2022/rust
parent5186f8d891c60d61d406779d2890770f9e41f0e7 (diff)
Day Seven: Switch to weak references
Diffstat (limited to '2022/rust')
-rw-r--r--2022/rust/day07/src/main.rs8
1 files changed, 4 insertions, 4 deletions
diff --git a/2022/rust/day07/src/main.rs b/2022/rust/day07/src/main.rs
index 1ea8840..8ed8d24 100644
--- a/2022/rust/day07/src/main.rs
+++ b/2022/rust/day07/src/main.rs
@@ -2,7 +2,7 @@ use std::cell::{Cell, RefCell};
use std::cmp;
use std::env;
use std::fs;
-use std::rc::Rc;
+use std::rc::{Rc, Weak};
struct Directory {
id: String,
@@ -10,7 +10,7 @@ struct Directory {
// mmm i'd rather have a plain Directory instead of Rc<Directory>
// but i get "cannot move out of x which is behind a shared reference"
children: RefCell<Vec<Rc<Directory>>>,
- parent: Option<Rc<Directory>>
+ parent: Option<Weak<Directory>>
}
fn main() {
@@ -33,7 +33,7 @@ fn main() {
// println!("$ ls")
},
["$", "cd", ".."] => {
- current = current.parent.clone().unwrap();
+ current = current.parent.clone().unwrap().upgrade().unwrap(); // lmao
// println!("$ cd ..");
},
["$", "cd", id] => {
@@ -52,7 +52,7 @@ fn main() {
id: String::from(id),
size: Cell::from(0),
children: RefCell::from(Vec::new()),
- parent: Option::from(current.clone())
+ parent: Option::from(Rc::downgrade(&current))
}));
// println!("dir {}", id)
},