Search code examples
rustreferenceimmutabilitymutableborrow-checker

cannot borrow `win` as immutable because it is also borrowed as mutable


I am getting a cannot borrow win as immutable because it is also borrowed as mutable as the commented line

let (mut win, thread) = raylib::init().size(800, 600).title("Demo").build();
// error at borrowing win
draw.draw_rectangle(
            // here
            win.get_screen_width() / 2,
            0,
            5,
            // and here
            win.get_screen_height(),
            Color::WHITE,
);

Solution

  • I think there are two possibility (you do not provide enough ressources to test it in my workspace so it is just supposition) :

    Save length before calling :

    let (mut win, thread) = raylib::init().size(800, 600).title("Demo").build();
    let width = win.get_screen_width() / 2;
    let height = win.get_screen_height();
    draw.draw_rectangle(width, 0, 5, height, Color::WHITE);
    

    Or you could just clone your instance :

    let (mut win, thread) = raylib::init().size(800, 600).title("Demo").build();
    draw.draw_rectangle(win.clone().get_screen_width() / 2, 0, 5, win.clone().get_screen_height(), Color::WHITE);
    

    Hope this help (and works ^^')