DEV Community

Cover image for A Rust developer's love life

A Rust developer's love life

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));
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)