VOOZH about

URL: https://dev.to/exlee/a-rust-developers-love-life-2pe3

⇱ A Rust developer's love life - DEV Community


struct Heart;

trait Love {
 fn fall_in_love(&self) -> Result<Heart, Rejection>;
}

impl Love for Heart {
 fn fall_in_love(&self) -> Result<Heart, Rejection> {
 // The compiler guarantees safety.
 // Unfortunately, it can't guarantee commitment.
 Err(Rejection::MovedOn)
 }
}

fn main() {
 let me = Heart;
 let you = Heart;

 // me & you; // ERROR: cannot borrow `you` as mutable more than once

 match me.fall_in_love() {
 Ok(_) => println!("happily ever after"),
 Err(_) => loop {
 // Rust prevents use after free.
 // But it can't prevent feeling after free.
 std::thread::sleep(std::time::Duration::from_secs(86400));
 }
 }
}

The punchline? Even after the heartbreak, the borrow checker won't let you let go -- you is still borrowed.