3

There is any common pattern to implement something like this in Rust?

The error is

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

I understand the problem but have no idea how to implement something like this in Rust.

struct Sprite { position: i32, } impl Sprite { pub fn left(&mut self) { self.position += 1; } } struct Game<'g> { sprite: &'g Sprite, } impl<'g> Game<'g> { pub fn new(sprite: &Sprite) -> Game { Game { sprite: sprite } } } fn main() { let mut sprite = Sprite { position: 3 }; let game = Game::new(&sprite); sprite.left(); } 

The code is also available on the playground.

5
  • 1
    Welcome to Stack Overflow! This is an extremely common question; there are now 94 questions with the same error message. SO expects that you have put a lot of effort into your question before asking, so you should take the time to read through those existing questions, then explain how this question is different from those. If people don't read existing answers, there's not much reason to have a site like SO. Commented Mar 29, 2017 at 20:48
  • Additionally, I'd highly suggest using more prose to describe your problem in the body. This will also naturally translate into a better title, which is invaluable. Saying "something like this" forces a future answer searcher to read, parse, and understand your code before being able to understand if the solutions will be useful to them. It forces the same effort on every potential answerer, slowing down or otherwise discouraging any potential answerers. Commented Mar 29, 2017 at 20:54
  • 1
    Adding on, it's a very good idea to read through the free online documentation provided by the Rust community. The Rust Programming Language explains the rationale for many of Rust's decisions, as well as showcasing examples. The chapter on references and borrowing covers the fact that you may not have any other references at the same time you have a mutable reference, for example. Commented Mar 29, 2017 at 20:58
  • 1
    @fazibear for questions like this, I recommend asking on IRC. People are super nice and helpful, and no one gets up in your face about "this question has been asked before". Commented Mar 29, 2017 at 22:56
  • There's information on accessing Rust's IRC on Rust's website. Additional resources include the user's forum and the Rust subreddit, which has a weekly "easy question" thread that is always full of helpful people. Commented Mar 29, 2017 at 23:32

1 Answer 1

2

Intuitively, Games should probably own their Sprites. Here is an updated version reflecting that design change. Also on the playground.

struct Sprite { position: i32, } impl Sprite { pub fn left(&mut self) { self.position += 1; } } struct Game { sprite: Sprite, } impl Game { pub fn new(sprite: Sprite) -> Game { Game { sprite: sprite } } } fn main() { let sprite = Sprite{ position: 3 }; let mut game = Game::new(sprite); game.sprite.left(); } 
Sign up to request clarification or add additional context in comments.

2 Comments

And if Sprite ends up being this huge structure that you don't want to copy around, you can put them in a Box.
Updated code using Box

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.