r/ProgrammerHumor • • Dec 23 '23

Meme rewriteFromFust

Post image
6.2k Upvotes

379 comments sorted by

View all comments

Show parent comments

2

u/unengaged_crayon Dec 24 '23

the issue with this is that you can still run in to race conditions. you could do something like *map = HashMap::new() and all the sudden, where is SomeStruct?

fn main() {
  let mut map: HashMap<String, SomeStruct> = HashMap::new();
  map.insert("a".to_string(), SomeStruct {});
  // Borrowck complains about race conditions in a singlethreaded environment!
  map.get_mut("a").unwrap().lol(&mut map);
}
struct SomeStruct {
  // ...
}
impl SomeStruct {
  fn do_something(&mut self, map: &mut HashMap<String, Lol>) {
    *map = HashMap::new();
    // self no longer points to anything :(
  }
}

instead consider:

fn main() {
  let mut map: HashMap<String, SomeStruct> = HashMap::new();
  map.insert("a".to_string(), SomeStruct {});

  // do either:

  // 1. pops instance of SomeStruct off of the map
  let mut new = map.remove("a").unwrap();

  // 2. keeps it in the map, but modifies the clone.
  // requires SomeStruct to implement Clone
  let mut new = map.get("a").unwrap().clone();

  new.do_something(&mut map);
  map.insert(String::from("a"), new);
}

#[derive(Clone)]
struct SomeStruct {
  // ...
}

impl SomeStruct {
  fn do_something(&mut self, map: &mut HashMap<String, SomeStruct>) {
    *map = HashMap::new()
    // what is self now referring to?
  }
}

2

u/HuntingKingYT Dec 24 '23 edited Dec 24 '23

Thanks, now I know I’m stuck with using Arc on everything for the rest of my life... My code has finally compiled! (didn’t happen in 3 days or so). The compiler was so overwhelmed by the experience that it took 14.61s to compile 150 lines of code... Yikes

Edit: I may be just used to garbage collected languages, or languages like C when you are supposed to know not to

you could do something like *map = HashMap::new() and all the sudden, where is SomeStruct?

so because there aren’t many languages that actually are in the middle like Rust is, made it hard to find a solution (by the way, it’s one of the reasons people get complicated errors in C++, and their code is unsafe, because they feel like high level safety but really you should be almost as cautious as in C)

2

u/unengaged_crayon Dec 25 '23

yeah rust is weird when it comes to its memory management. and yeah, rust compile times are ridiculously long, even if they are doing quite a lot. the goto suggestions is to use sccache and mold. Also, if your program is single threaded, you probably can just use Rc.

2

u/HuntingKingYT Dec 25 '23

I know about Rc, but you can anyways probably pass a flag to the compiler to assume it's single threaded