0

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,
);
6
  • can you please share the error message as well Commented Jul 30, 2022 at 4:34
  • The example is incomplete. What's draw, and what version of raylib are you using? Commented Jul 30, 2022 at 4:43
  • You need to provide more information. A minimal reproducible example will give people a chance of helping you. Commented Jul 30, 2022 at 6:52
  • But, making a lot of guesses about the rest of your code, you can probably fix this by introducing new variables, width and height before the method call, rather than doing it all inline. Commented Jul 30, 2022 at 6:55
  • The code you are showing does not contain the actual problem. win.get_screen_width() is an immutable borrow, and so is win.get_screen_height(). This should compile fine. The actual error comes from code you don't show here. I agree with @PeterHall, please provide a minimal reproducible example. Commented Jul 30, 2022 at 8:00

1 Answer 1

0

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 ^^')

Sign up to request clarification or add additional context in comments.

2 Comments

OP really needs to add more information before this can be answered. This solution is unlikely to work because the error suggests that draw_rectangle takes &mut self and that draw must hold a mutable reference to win too. In that case you would have to introduce the width and height variables before draw was assigned.
This worked after destructuring my code for getting width and height into variables rust let (mut win, thread) = raylib::init().size(800, 600).title("Demo").build(); let win_width = win.get_screen_width(); let win_height = win.get_screen_height(); // then while calling draw draw.draw_rectangle(win_width / 2, 0, 5, win_height, Color::WHITE);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.