I'm strugglin' with the borrow checker - wonder o' wonder.
While I found a solution by adding a block, I am curious if there are other ways to end a mutable borrow so the next statement can access a binding afterwards.
Here's what I did so far:
let mut canvas: Canvas = Canvas {
width: 5,
height: 5,
array: vec!['x'; 5*5],
};
{
let mut renderer: CanvasRenderer = CanvasRenderer::new(&mut canvas);
renderer.render_point('x', 3, 3);
}
println!("The Value in the array is: {}", canvas.array[9]);
I wrap a block around the binding of a CanvasRenderer
object and after mutating the canvas and the scope ends, the CanvasRenderer
dies and my mutable borrowed canvas
is available to be read or whatever.
This works - but now I'd like to see other solutions!
I heard about drop(stuff)
but it did not work as I thought it should.