[Media] There actually are two bugs in this code

444 points Aaron1924 19 comments
[Media] There actually are two bugs in this code

I have seen this meme quite a number of times on different platforms, and I was curious if this was just a random Rust code snippet or if there is actually a bug here

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=4671c970db7299c34f420c2d4b891ceb

As it turns out, this does not compile for two reasons!

  1. !if p.borrow().next == None { break; } does not work because Node does not implement PartialEq. This can be fixed by either deriving the trait or using .is_none() instead.!<

  2. !p = p.borrow().next.clone().unwrap(); does not pass the borrow checker because p is borrowed twice, once immutably by the right-hand side and once mutably by the left-hand side of the assignment, and the borrow checker does not realize the immutable borrow can be shortened to just after the call to .clone(). This can be fixed as follows: p = {p.borrow().next.clone()}.unwrap();!<

So the correct response to the captcha is to click >!the two boxes in the middle row!!<