Stream: general

Topic: &mut Type doesn't implement Copy?


Luca Barbato (May 10 2019 at 11:19, on Zulip):

here a playground

Luca Barbato (May 10 2019 at 11:22, on Zulip):

I'm trying to remove some spurious dyn around some code and I found that doing

{
    let arg = if foo { one } else { another };
    call(arg);
}
    other_call(one);
    other_call(another);

triggers that

oli (May 10 2019 at 11:43, on Zulip):

You can fix this by reborrowing (&mut *some_reference): https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f3983e679905e80db4832868e9bd00dc

oli (May 10 2019 at 11:43, on Zulip):

The problem is that &mut T is indeed not Copy, and it cannot be, otherwise you could easily create two &mut T to the same memory by just copying

oli (May 10 2019 at 11:44, on Zulip):

but you can create a new reference that borrows the original reference, then consume that new reference and the old one still exists

Luca Barbato (May 10 2019 at 11:49, on Zulip):

the strange thing is:

  let arg : &mut Foo = if foo {.. } ...

Works as well...

RalfJ (May 10 2019 at 11:57, on Zulip):

probably there's reborrowing going on

RalfJ (May 10 2019 at 11:57, on Zulip):
let x = &mut 0;
foo(x); // sugar for "foo(&mut *x)"
foo(x); // works
foo({x}); // the braces defeat the reborrowing
foo(x); // does not work
Luca Barbato (May 10 2019 at 11:59, on Zulip):

I see :) Now it is clear

Last update: Nov 22 2019 at 00:35UTC