Borrow checker limitation?

⚓ Rust    📅 2025-09-23    👤 surdeus    👁️ 9      

surdeus

Warning

This post was published 38 days ago. The information described in this article may have changed.

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Borrow checker limitation?

I'm trying to code something that looks like the following code, but I get an errors saying that self is already borrowed when I'm calling the second method.

I don't understand what happen here, the code seams correct. Also is there any workaround for this?

(playground)

struct Foo;

impl Foo {
    fn get_mut(&mut self) -> Option<&mut str> {
        if let Some(x) = self.find_1_mut() {
            return Some(x);
        }
        
        if let Some(x) = self.find_2_mut() { // Error: `self` already borrowed
            return Some(x);
        }
        
        None
    }

    fn find_1_mut(&mut self) -> Option<&mut str> {
        todo!();
    }
    
    fn find_2_mut(&mut self) -> Option<&mut str> {
        todo!();
    }
}

1 post - 1 participant

Read full topic

🏷️ Rust_feed